ASP.NET表格控件 - 保存按钮回发丢失了我的所有行和单元格

问题描述 投票:0回答:1

我有一个ASP.NET表控件,我已将其拖到我的页面。我必须动态创建行和列,我也做过。我表中的一些单元格是文本框。我希望能够修改文本框值并保存更改。我在与表格相同的面板中添加了一个按钮,但是当我尝试遍历表格行时,我得到零。

为什么表不记得我的行和单元格?因为我已经加载了数据,所以每次按下按钮时我都不想重建表格。我想通过每次在Page_Load中创建表,我会来回到数据库服务器。我真的想避免这种情况。

asp.net postback
1个回答
2
投票

您可以将动态创建的表存储在可以在渲染后获取的视图中。

SqlConnection con =new  SqlConnection(ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString);

private int ctrName = 0;
Table tableName = null;  

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        tableName = new Table();
        tableName.ID = "tableName";
        Session["tableName"] = tableName;
        ViewState["ctrName"] = ctrName;
    }
    ctrName = (Int32)ViewState["ctrName"];
    tableName = (Table)Session["tableName"];
    pnlName.Controls.Add(tableName);
}
private void GenerateName()
{
    ctrName++;
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    TextBox tb = new TextBox();
    tb.ID = "Txt_Name" + ctrName;
    tb.Width = 150;
    cell.Controls.Add(tb);
    row.Cells.Add(cell);
    tableName.Rows.Add(row);
    Session["tableName"] = tableName;
    ViewState["ctrName"] = ctrName;
}

对于Access表格详细信息:

tableName = (Table)Session["tableName"];
© www.soinside.com 2019 - 2024. All rights reserved.