8

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?

brain_pusher
  • 1,507
  • 3
  • 21
  • 31
  • 1
    I am not sure what you are doing wrong. Do you have to use the PreInit event? Can you use a later event like Init? – Matthew Jones Jul 01 '10 at 15:22
  • 1
    Like Matthew says a later event might suit you better. I suspect that its in init that a lot of the controls actually get put together so if you try to access things too early in the lifecycle it all falls apart. http://msdn.microsoft.com/en-us/library/ms178472.aspx might be useful if you havent' already seen it. – Chris Jul 01 '10 at 15:27
  • Having read your edit I generally add stuff to the control tree in the page_load event. Not sure what that section you quoted is referring to to be honest but I don't think it means just adding a link to a page... – Chris Jul 01 '10 at 15:44

4 Answers4

5

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
  • 6,773
  • 3
  • 24
  • 45
1

In the PreInit() event, standard (defined) controls have not been created yet at that stage, thus you cannot get any reference to any controls.

Use the Page_Load() event to create dynamic controls. During this event, you can add any dynamic controls into existing controls.

After creating the dynamic controls in Page_Load(), use the PreRender() to make any changes/updates.

Personally, I use PreInit to define page wide objects (ie, database connections, user session objects) and also where I perform security authentication checks (and redirects if not authorized) only.

jlee88my
  • 2,935
  • 21
  • 28
1

The Page's PreInit event is triggered before the controls are initialized, so controls do not exist yet. You will have to access the control in a later event, such as the Page's Load event. Please see http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Halcyon
  • 14,631
  • 17
  • 68
  • 99
0

There's a difference between Pages with or without a Master Page as explained HERE in the Question and Answers.

Without a Master Page you can create controls in the PreInit event and add them to an existing control but with a Master Page you cannot access the existing controls yet, as explained in the answers here, so you have to create dynamic controls in a later event like the Init event.

Since you are using a Master Page you should create your dynamic controls in a later event like the Init event or try the option given there by Valio.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46