UpdatePanel 不保留我的动态控件

UpdatePanel don't keep my dynamic controls

我在 UpdatePanel 中有一个 table,在这个 Table 下有一个动态添加新行的按钮。当我第一次按下按钮时,会生成一行。但是,如果我第二次点击该按钮,则会生成一个新行,但第一行会消失。

我不太熟悉 UpdatePanel 我不知道它们是否正确:

这是我的 HTML 代码:

<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>

还有我的 C# 代码:

    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);
    }

看来你做不到

来自 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.

更多信息here