Init Event Does Not Fire for Any Control

This one is a little weird.

Normally, attaching an Init event handler to a control is pretty straight-forward. Standard, typical stuff. However, we encountered a situation where the init event handlers were not firing for any control on our page.

The page is actually an MS CMS template, but really they are just ASP.NET web pages. Usually these templates inherit from System.Web.UI.Page, but we decided to do the 1.1 "master" page trick by inheriting from another class "BaseTemplate", which in turn inherited from another class "BasePage" which finally inerits from the base Page class.

I don't think this chain of inheritance is the cause, but I do know that the problem is a result of "order of events". From what I understand, the init events in our set up were occuring too late. In other words, the Page Load event had already fired, so the control Init events can't fire (even though their Load events do...); something like the child's OnInit getting called before the page's OnInit event.

The solution was to use the OnInit attribute in declaritive code like this:
<asp:Literal id="myLiteral" OnInit="myLiteral_Init" ... />
The Init method would have to change from the normal "private" to "protected".

An explanation as found in the news groups:
the OnInit event of the Page is too late for registering event handler for sub control(added statically)'s Init event). Because all the static sub control's Init event is fired when they are being added into the parent container which is before the Page's OnInit function. You can try using the suggetion on registering the event handler in aspx page inline.

Worth noting: BUG: Imperatively Wired Control Event Does Not Fire in Visual C# .NET Web Application (http://support.microsoft.com/?id=314965)



Published Friday, November 25, 2005 9:52 PM by Puckboy
Filed under

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

Name  

Comments 
Type the characters you see in the picture on the left.
    



Note: HTML tags in comment messages are not supported.