Published: 2009-06-11 06:38:27
Video Summary:
Overview on How to Implement a Simple Event Handler in OnInit, OnPreRender & OnUnload Methods.
Video Tags:
aspnet page method, visual studio 2005, Microsoft Windows, web development, total training
Source: How to Implement a Simple Event Handler Method
Video Transcript: (
More)
Let us close the browser and I want to just discuss one or two more things in this example before we move on and look at the event model in more detail. We have so far looked at the page load method and we have seen how to implement a simple event handler method. As it turns out, the page load method is one of many methods that you can implement in the page class. So, to look at some of the other methods, let us have a look at the help system.
On Help, click Index and in the Index, let us type Page Class. Okay, so this displays help information about the page class. If we want to, we can have a look at just the methods and the various methods I am interested in are actually protected methods. So, let us collapse the public methods down and let us just look at the protected methods. In particular, I am going to scroll down the list. And the three methods I want to look at are the OnInit method and then OnPreRender Method and the OnUnload method.
So, I will just discuss the OnInit method first of all. The OnInit method is called after the page object has been created from the aspx file but before asp.net has updated the control with View state. So, when you implement the OnInit method, you cannot access any up-to-date data in the controls. So, you might be wondering what would be the purpose of implementing the OnInit method in your page class. Well, let us go back to Visual Studio and before the page load method, I’ll just give myself a bit more space here. I am going to implement the OnInit method. So, it is a protective method and I am just going to overwrite and from here, I can choose which method I want to overwrite. So, I need to scroll down the list and find the OnInit method. There it is. And when we implement that method, you can Visual Studio has automatically called the OnInit method in the base class which actually triggers the Init event on the page.
If you have any delegates written on that Init method, they will be notified by the event. So, it is important to keep that method call in there unless you want to, you can perform some additional initialization. Now remember, at this stage you cannot access the controls because the view state information has not been ‘decivilized’ yet to initialize the controls properly. So really, the only type of initialization you can perform is programmatic initialization of instance variables. So as an example, I am going to go up a few lines at the top of the class and I am going to add a private instance variable which is a date, time object called time stamp and in the OnInit method, I am going to initialize that variable, going to current date and time.
Okay. And that is finally typical of the kind of initialization that you perform in the OnInit method. It is very definitely non-graphical. Okay. After the OnInit method has finished, asp.net then ‘decivilizes’ view state and adds in any additional form data such as the new text that the users entered in the text box and things like that. And then it calls the page load method. And obviously we have just discussed the page load method and quite a little of the detail already. I do need to change the page load method here so that rather than getting the date time stamp via programmatic code, we just pick up the time stamp variable that we set up earlier. Okay, so that is the page load method. After page load, asp.net calls the various event handlers such as the View button click as we have discussed previously then just before asp.net tells each control to render itself asHTML and to open itself as View state, Asp.net triggers the PreRender event or to be more specific, it calls the OnPreRender method. So, I am going to overwrite that method here, protect it, overwrite and we are going to overwrite the OnPreRender method. So, this method gets called after all the event handlers have fired. So, this is a good place for you to perform any tidy up operations on your controls just before they render themselves as HTML. This is your last place to change the state of the objects such that it affects the HTML that is about to be rendere