C# 在page_init中动态创建控件并在click事件中访问
我在C# 在page_init中动态创建控件并在click事件中访问,c#,asp.net,vb.net,C#,Asp.net,Vb.net,我在page_Init事件中创建了一个动态表。如何在click事件处理程序中访问该表?当前无法在单击事件处理程序中访问它。实际上,我需要遍历已添加到该表中的其他动态控件的动态创建表。在页面中声明私有局部变量,如\u table As… 然后,在Page_Init中,使用这些变量\u table=New… 您可以在页面生命周期的后期使用\u table。您可以通过它的ID访问它,从而分配一个唯一的ID(在他的命名容器中) 在aspx上: <asp:Panel id="TableContain
page_Init
事件中创建了一个动态表。如何在click事件处理程序中访问该表?当前无法在单击事件处理程序中访问它。实际上,我需要遍历已添加到该表中的其他动态控件的动态创建表。在页面中声明私有局部变量,如\u table As…
然后,在Page_Init中,使用这些变量\u table=New…
您可以在页面生命周期的后期使用
\u table
。您可以通过它的ID
访问它,从而分配一个唯一的ID(在他的命名容器中)
在aspx上:
<asp:Panel id="TableContainer"
HorizontalAlign="Center"
runat="server">
<!-- put your table(s) here -->
</asp:Panel>
protected void Page_Init(Object sender, EventArgs e)
{
var table1 = new Table();
table1.ID = "Table1";
for(int rowCtr = 1; rowCtr <= 10; rowCtr++) {
// Create new row and add it to the table.
TableRow tRow = new TableRow();
table1.Rows.Add(tRow);
for (int cellCtr = 1; cellCtr <= 10; cellCtr++) {
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tCell.Text = "Row " + rowCtr + ", Cell " + cellCtr;
tRow.Cells.Add(tCell);
}
}
TableContainer.Controls.Add(table1);
}
但是,您应该考虑使用webdatabound控件,如Repeater
或GridView
。您需要显示一些代码,并告诉我们您已经尝试了什么
protected void button1_Clicked(Object sender, EventArgs e)
{
Table table1 = (Table) TableContainer.FindControl("Table1");
}