I have code like this
protected void Button1_Click(object sender, EventArgs e)
{
Table t = new Table() ;
t.ID = "T1";
t.Visible = true;
MyPanel.Controls.Add(t);
}
protected void Button2_Click(object sender, EventArgs e)
{
Table t = (Table)MyPanel.FindControl("T1");
}
I have problem there: Table t = (Table)MyPanel.FindControl("T1");
In variable t, there is now reference to null. I looks like the aplication doesn't know about code-behind generated table. Do I have to "Register" the table somewhere else than only to the MyPanel? Thank you for the answers.
EDIT More information about the problem
I have simple page without masterPage.There is Gridview with a numbers. This numbers mean how many rows and cells the new TAble will have. When the user select a row, I want to create appropriate Table and after click on second button I need collect information from the table. OnInit or PreRender I don't know, how much rows I will need. Does it mean my problem is technically impossible?
You have Panel1 but you are searching a table called MyPanel ??
protected void Button2_Click(object sender, EventArgs e)
{
Table t = (Table)Panel1.FindControl("T1");
}
This should get you the table
EDIT
Ok I dug up some old webForms stuff that does exacly this.
BUT you have to add your table on the initialize or preInit event of the page, if you want to interact with it in code later. And you have to Recreate the conrol on each postback.
public static Control FindControlRecursive(Control root, string id)
{
if (root.id == id)
return root;
foreach (Control ctrl in root.Controls)
{
Control FoundCtl = FindControlRecursive(ctrl, id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
And you can use it lik this.
table myTable = FindControlRecursive(this.Master, "T1") as Table