views:

22

answers:

1

How to get control in ASP.NET PreInit event? Pointers are null and FindControl method returns null.

I am using master and content pages. Markup of the content page looks like this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentBody" runat="server">
   <asp:Table ID="Table1" runat="server" Width="100%">
      .....
   </asp:Table>
</asp:Content>

And code like this:

private void Page_PreInit(object sender, EventArgs e)
{
    Control table = this.FindControl("Table1");
    //table is null here
}

So table still is null after this.FindControl("Table1"). NamingContainer of the page is null too. What am I doing wrong?

UPDATE I have to use this event to create controls. As said in the ASP.NET Page Life Cycle Overview this event should be used for dynamic control creation. I need to create a list of links in my table. May be there is another way to do it?

A: 

PreInit is fired before the controls are initialized. Read up on the ASP.NET Page Life Cycle for more detailed information.

Init
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.

Use this event to read or initialize control properties.

Shawn Steward
See my update .
brain_pusher