c# - ASP.NET 动态生成的 TableRows 不会在回发之间持续存在

标签 c# asp.net

在 ASPX 中

<asp:Table ID="superTable" runat="server" Width="100%">
    <%--populate me on the fly!--%>
</asp:Table>

<asp:Button ID="btnAddRow" runat="server" CausesValidation="false" Text="Add Row" onclick="btnAddRow_Click" Width="90%"/>

<asp:Button ID="btnRemoveRow" runat="server" CausesValidation="false" Text="Remove Last Row" onclick="btnRemoveRow_Click" Width="90%"/> 

<asp:Button ID="btnSubmit" runat="server" Text="1" onclick="btnSubmit_Click"  Width="90%"/>

CodeBehind 的相关位

protected void Page_Load(object sender, EventArgs e)
    {if (!IsPostBack){ writeHeader(); makeMeARow(); }}

protected void btnAddRow_Click(object sender, EventArgs e)
{   
    if (int.Parse(btnSubmit.Text) <= 20)
    {   int b = superTable.Rows.Count+1;

        writeHeader();
        btnSubmit.Text = (int.Parse(btnSubmit.Text) + 1).ToString();

        for (int a = 1; a <= int.Parse(btnSubmit.Text); a++)
            { makeMeARow(); }
    }
    else{/*tell user they can't do that! Max of 20 rows as noted by form requirements */}
}

private void writeHeader()
{
    //= == create row == =//
    TableHeaderRow tempHeaderRow = new TableHeaderRow();//make row

    //= == create cells == =//
    TableHeaderCell tempHeaderCell01 = new TableHeaderCell();
    TableHeaderCell tempHeaderCell02 = new TableHeaderCell();
    TableHeaderCell tempHeaderCell03 = new TableHeaderCell();

    tempHeaderCell01.Text = "Call Number";  tempHeaderCell01.Width = Unit.Percentage(33);
    tempHeaderCell02.Text = "Author";       tempHeaderCell02.Width = Unit.Percentage(33);
    tempHeaderCell03.Text = "Title";        tempHeaderCell03.Width = Unit.Percentage(33);

    //= == add TableCells to TableRow == =//
    tempHeaderRow.Cells.Add(tempHeaderCell01);
    tempHeaderRow.Cells.Add(tempHeaderCell02);
    tempHeaderRow.Cells.Add(tempHeaderCell03);

    //superTable.Rows.AddAt(superTable.Rows.Count, tempRow);
    superTable.Rows.Add(tempHeaderRow);
}

protected void btnRemoveRow_Click(object sender, EventArgs e)
{   int b = superTable.Rows.Count - 1;

    writeHeader();
    btnSubmit.Text = (int.Parse(btnSubmit.Text) - 1).ToString();
    for (int a = 1; a <= int.Parse(btnSubmit.Text); a++)
    {makeMeARow();}   
}
private void makeMeARow()
{
    //= == maybe off by one? == =//
    string rowCount = superTable.Rows.Count.ToString("00");

    //= == create row == =//
    TableRow tempRow = new TableRow();//make row

    //= == create cells == =//
    TableCell tempCell01 = new TableCell();
    TableCell tempCell02 = new TableCell();
    TableCell tempCell03 = new TableCell();

    //= == create TextBoxes == =//
    TextBox tempTextBox01 = new TextBox();
    TextBox tempTextBox02 = new TextBox();
    TextBox tempTextBox03 = new TextBox();

    //= == change the ID of TableRow == =//
    tempRow.ID = "tableRow_" + rowCount;

    //= == change the IDs of TableCells == =//
    tempCell01.ID = "tableCell_" + rowCount + "_01";
    tempCell02.ID = "tableCell_" + rowCount + "_02";
    tempCell03.ID = "tableCell_" + rowCount + "_03";

    //= == change the IDs of TextBoxes == =//
    tempTextBox01.ID = "txtCallNumber_" + rowCount;
    tempTextBox02.ID = "txtAuthor_" + rowCount;
    tempTextBox03.ID = "txtTitle_" + rowCount;

    //= == change TextBox widths to 90%;
    tempTextBox01.Width = Unit.Percentage(90);
    tempTextBox02.Width = Unit.Percentage(90);
    tempTextBox03.Width = Unit.Percentage(90);

    //= == add TextBoxes to TableCells == =//
    tempCell01.Controls.Add(tempTextBox01);
    tempCell02.Controls.Add(tempTextBox02);
    tempCell03.Controls.Add(tempTextBox03);

    //= == add TableCells to TableRow == =//
    tempRow.Cells.Add(tempCell01);
    tempRow.Cells.Add(tempCell02);
    tempRow.Cells.Add(tempCell03);

    //add TableRow to superTable
    //superTable.Rows.AddAt(superTable.Rows.Count, tempRow);
    superTable.Rows.Add(tempRow);
}

好的,所以,我的问题; - 当我点击“添加行”或“删除行”按钮时,单元格中的数据不会在回发之间保留。相关的行和单元格拥有相同的 ID,但不会保留数据。为什么不呢?

最佳答案

动态控件必须在每次回发时重新添加到表单中。通常这是在页面生命周期的 Init 阶段完成的。您动态添加的控件实际上具有 ViewState。当使用与之前完全相同的 ID 将适当的控件重新添加到控件树时,它应该会重新出现,并带有 ViewState 中保留的值。

查看 this article有关使用动态控件的简单提示,或者您可以从 4 Guys from Rolla 查看本教程以获得更深入的了解。

关于c# - ASP.NET 动态生成的 TableRows 不会在回发之间持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243516/

相关文章:

c# - 扩展方法在内部是如何实现的

c# - 单选按钮上的模型绑定(bind)为该属性返回 null (ASP.NET Core)

c# - 自动滚动并导航到 asp.net div

asp.net - session 未清除

c# - 如何构建一个健壮/可扩展的异步等待回显服务器?

c# - 在 F# 中声明私有(private)静态成员?

c# - 为什么将动态对象类型转换为对象会抛出空引用异常?

c# - 使用 Json.net 将 JSON 对象反序列化为动态对象

asp.net - 在 ASP.NET 中填充文本框而不重新加载页面?

asp.net - 在 SELECT 子句中显示不同 COUNT 的存储过程