0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Foxxed
  • 13
  • 1
  • 5
  • 1
    first of all you are adding a table to Panel1 and finding it in MyPanel. How will you find the table? – Sain Pradeep Sep 16 '13 at 07:22
  • Just in case your sample isn't a complete representation, note that `FindControl` searches only children 1 level deep - you may need to recurse to find deeper child controls - see http://stackoverflow.com/questions/8589950/findcontrol-return-null – StuartLC Sep 16 '13 at 07:29
  • But I know here exactly, what is parent and child, or am I wrong? – Foxxed Sep 16 '13 at 09:22

3 Answers3

1

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
Archlight
  • 2,019
  • 2
  • 21
  • 34
  • Are you using master pages? And are the button1 and button2 in the same page/cs file ? – Archlight Sep 16 '13 at 09:27
  • 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? – Foxxed Sep 16 '13 at 13:20
  • It is absolutely possible. You just have to do some manual stuff. You just have to acept that the hardcoded onClick stuff won't work, but you can check in the Initialize event for the Id of the control who did the postback (String ButtonID = Request["__EVENTTARGET"];) and then render the table and after rendering the table you know the information and can build your detail view or whatever you are doing. – Archlight Sep 17 '13 at 07:24
0

do your code OnPreRender

protected override void OnPreRender(EventArgs e)
    {
            Table t = new Table() ;
            t.ID = "T1";
            t.Visible = true;
            MyPanel.Controls.Add(t);
    }
-3

ctl00$ContentPlaceHolder1$UrunlerRAjax1$rptCustomers

contentplaceholder-->usercontrol-->repeater

Repeater rptCustomers = this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("UrunlerRAjax1").FindControl("rptCustomers") as Repeater;

Repeater rptCustomers = this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("UrunlerRAjax1").FindControl("rptCustomers") as Repeater;

Repeater repeaterName = this.Page.Master.FindControl("ContentName").FindControl("UsercontrolName").FindControl("repeaterName") as Repeater;

Null29
  • 1
  • 1
  • This is not a social network to greet people! Please format your answers based on the guidelines and provide proper, high quality details for an answer you want to write. – Neeku Jun 04 '14 at 11:35