0

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

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

4

1 回答 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;
}

对于访问表详细信息:

tableName = (Table)Session["tableName"];
于 2012-09-07T09:45:29.030 回答