How to get control in ASP.NET PreInit event?
Asked Answered
S

4

9

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?

Shake answered 1/7, 2010 at 15:18 Comment(3)
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?Kreager
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. msdn.microsoft.com/en-us/library/ms178472.aspx might be useful if you havent' already seen it.Prophylaxis
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...Prophylaxis
C
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.

Craniometry answered 1/7, 2010 at 15:26 Comment(0)
P
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.

Phanotron answered 23/3, 2013 at 15:27 Comment(0)
F
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.

Fainthearted answered 19/5, 2014 at 18:45 Comment(0)
V
1

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.

Verde answered 3/1, 2019 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.