ASP.NET动态生成TableRows不会回发之间仍然存在 [英] ASP.NET dynamically-generated TableRows won't persist between postbacks

查看:122
本文介绍了ASP.NET动态生成TableRows不会回发之间仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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%"/>

C $ cBehind的$相关位

Relevant bits of 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,但不会持续存在的数据。为什么不呢?

Okay, so, my problem; -when I hit either the "Add Row" or "Remove Row" button, the data in the cells don't persist between postbacks. The relevant rows and cells hold the same IDs, but don't persist data. Why not?

推荐答案

动态控件必须重新添加到每个回发的形式。这通常是在页面生命周期的初始化阶段完成。你已经动态添加控件实际上确实有ViewState中。当使用精确相同的ID它之前,必须适当控制重新添加到控制树,它应与在视图状态被持续的值重新出现。

Dynamic controls must be re-added to the form on each postback. Typically this is done during the Init phase of the page's lifecycle. The controls that you have added dynamically DO actually have ViewState. When the appropriate control is re-added to the control tree using the exact same ID it had before, it should reappear with the values that were persisted in ViewState.

查看关于使用动态简单的提示这篇文章控制或者你可以检查出从本教程 4,距离Rolla 家伙更-depth的样子。

Check out this article for simple tips on using dynamic controls or you can check out this tutorial from 4 Guys from Rolla for a more in-depth look.

这篇关于ASP.NET动态生成TableRows不会回发之间仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆