Bonjour à tous,

N'hésitez pas à déplacer mon post si il n'est pas au bon endroit

Je cherche à créer un tableau dynamique, c'est à dire à partir de contrôles utilisateur ajouter et enlever des lignes.

Pour cela, j'ai fait quelques tests...

Dans ma page aspx, j'ai:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<table id="Tab1" runat="server">
            <tr>
                <td align="center">
                    <h3>
                        Coucou</h3>
                </td>
                <td>
                    <asp:DropDownList ID="ddpReturnType" runat="server">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="ddpKindProduct1" name="ddpKindProduct1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdpKindProduct_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="ddpProduct1" runat="server">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <asp:Button ID="BtnAdd" Text="Ajouter Ligne" runat="server" 
            onclick="BtnAdd_Click" />
        <asp:Button ID="BtnConfirm" Text="Envoyer" runat="server" />
Dans ma page aspx.cs, j'ai :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void BtnAdd_Click(object sender, EventArgs e)
    {
        HtmlTableRow newRow = new HtmlTableRow();
 
        // Pour chacune des 3 colonnes
        for (int i = 0; i < 3; i++)
        {
            TextBox tb = new TextBox();
            HtmlTableCell cell = new HtmlTableCell();
            switch (i)
            {
                case 0:
                    cell.Controls.Add(tb);
                    break;
                case 1:
                    cell.Controls.Add(tb);
                    break;
                case 2:
                    cell.Controls.Add(tb);
                    break;
                case 3:
                    cell.Controls.Add(tb);
                    break;
                default:
                    break;
            }
            newRow.Cells.Add(cell);
        }
 
        Tab1.Controls.Add(newRow);
    }
}
Quand je click une fois sur le bouton add, il me rajoute bien une ligne. Après, il n'en rajoute plus.

Comment ça se fait ?

Merci de vos réponses