Search code examples
asp.netupdatepanelpostbackajaxcontroltoolkit

UpdatePanel don't keep my dynamic controls


I have a table in an UpdatePanel, and under this Table a button who dynamically add new rows. When I press the button for the first time, a row is generated. But if I click on the button a second time, a new row is generated but the first row disappear.

I am not very good with UpdatePanel I don't know if they are correct :

This is my HTML code :

<asp:UpdatePanel ID="UpdatePanel5" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
            <asp:Table ID="TableEmail" runat="server" BorderColor="Black" BorderWidth="1">
                <asp:TableRow ID="EmailRow1" runat="server" BorderColor="Black" BorderWidth="1">
                    <asp:TableCell BorderColor="Black" BorderWidth="1"></asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Informations Concessionnaire</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Contacts</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Objectifs</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Retour</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Stock</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">SAV/QUA</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Cliquette</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Paiement</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Concurrence</asp:TableCell>
                    <asp:TableCell BorderColor="Black" BorderWidth="1">Photos</asp:TableCell>
                </asp:TableRow>
            </asp:Table>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="BtnAddEmail" runat="server" CssClass="boutons" Text="+ Ajouter un destinataire" OnClick="BtnAddEmail_Click" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="BtnAddEmail" />
    </Triggers>
</asp:UpdatePanel>

And my C# code :

    public void BtnAddEmail_Click(object sender, EventArgs e)
    {
        //Instanciation des composants
        TextBox TxtEmail = new TextBox();
        Label LblEmail = new Label();
        CheckBox ChkBox1 = new CheckBox();
        CheckBox ChkBox2 = new CheckBox();
        CheckBox ChkBox3 = new CheckBox();
        CheckBox ChkBox4 = new CheckBox();
        CheckBox ChkBox5 = new CheckBox();
        CheckBox ChkBox6 = new CheckBox();
        CheckBox ChkBox7 = new CheckBox();
        CheckBox ChkBox8 = new CheckBox();
        CheckBox ChkBox9 = new CheckBox();
        CheckBox ChkBox10 = new CheckBox();

        //Configuration des composants
        LblEmail.Text = "@rapido.fr";
        TxtEmail.ID = "TxtEmail1";
        ChkBox1.ID = "ChkBox1";
        ChkBox2.ID = "ChkBox2";
        ChkBox3.ID = "ChkBox3";
        ChkBox4.ID = "ChkBox4";
        ChkBox5.ID = "ChkBox5";
        ChkBox6.ID = "ChkBox6";
        ChkBox7.ID = "ChkBox7";
        ChkBox8.ID = "ChkBox8";
        ChkBox9.ID = "ChkBox9";
        ChkBox10.ID = "ChkBox10";

        //Instanciation des cellules
        TableCell Cell1 = new TableCell();
        TableCell Cell2 = new TableCell();
        TableCell Cell3 = new TableCell();
        TableCell Cell4 = new TableCell();
        TableCell Cell5 = new TableCell();
        TableCell Cell6 = new TableCell();
        TableCell Cell7 = new TableCell();
        TableCell Cell8 = new TableCell();
        TableCell Cell9 = new TableCell();
        TableCell Cell10 = new TableCell();
        TableCell Cell11 = new TableCell();

        //Configuration des cellules
        Cell1.Controls.Add(TxtEmail);
        Cell1.Controls.Add(LblEmail);
        Cell2.Controls.Add(ChkBox1);
        Cell3.Controls.Add(ChkBox2);
        Cell4.Controls.Add(ChkBox3);
        Cell5.Controls.Add(ChkBox4);
        Cell6.Controls.Add(ChkBox5);
        Cell7.Controls.Add(ChkBox6);
        Cell8.Controls.Add(ChkBox7);
        Cell9.Controls.Add(ChkBox8);
        Cell10.Controls.Add(ChkBox9);
        Cell11.Controls.Add(ChkBox10);

        //Instanciation de la ligne
        TableRow Row1 = new TableRow();

        Row1.Cells.Add(Cell1);
        Row1.Cells.Add(Cell2);
        Row1.Cells.Add(Cell3);
        Row1.Cells.Add(Cell4);
        Row1.Cells.Add(Cell5);
        Row1.Cells.Add(Cell6);
        Row1.Cells.Add(Cell7);
        Row1.Cells.Add(Cell8);
        Row1.Cells.Add(Cell9);
        Row1.Cells.Add(Cell10);
        Row1.Cells.Add(Cell11);

        //TableEmail.Rows.AddAt(TableEmail.Rows.Count - 1, Row1);
        TableEmail.Rows.Add(Row1);
    }

Solution

  • It seems you cannot do it.

    From MSDN:

    It is important to remember that any programmatic addition or modification of table rows or cells will not persist across posts to the server. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. In fact, if substantial modifications are expected, it is recommended that a DataList, DataGrid, or GridView control be used instead of the Table control. As a result, the Table class is primarily used by control developers.

    More information here.