0

我想动态创建一个带有标签的表格并通过按钮单击更改表格上的标签

我可以动态地为表格提供不同的标签,但是我不能更改表格中的标签

这是我的代码:

。CS

protected ovverride void createchildcontrol()
{
    table1.border=1;
}

int counter=1;

for (int i = 1; i <= 8; i++)
{
    row = new htmltablerow();
    for (int j = 1; j <= 5; j++)
    {
        label = new label() { id = "label" + counter.tostring() };
        cell = new htmltablecell();
        cell.innertext = label.text = "label" + counter.tostring();
        row.cells.add(cell);
        counter++;
    }
table1.row.add(row);
}
counter = 1;
cell.controls.add(label);
this.controls.add(table1);
}

我的按钮代码

protected void button1_click(object sender, eventargs e)
{
    label find = cell.findcontrol("label1") as label;
    find.text = "changed"; 
}

id="placeholder" runatserver="server"在另一页上有一个 div

我真的需要访问我生成的 HTML 表上的标签。

4

1 回答 1

0

您将需要在pre_init事件中重新创建表。只有这样它才能工作。

protected override void OnPreInit(EventArgs e)
{
   base.OnPreInit(e);
   //recreate your table here
}

您也可以在 Page Load 事件中添加 Controls 动态控件。但在这种情况下,控件的状态不能在 PostBacks 中持续存在。因此,在加载视图状态之前,动态添加的控件应该存在于页面控件层次结构中。因此,在 Init 事件中创建控件使控件在层次结构中可用,以便它可以保持 ViewState。

于 2014-10-13T04:50:24.203 回答