So, you can see that each time we have posted back to the server, it adds to the label text. But how can this be? Http, the web protocol is a stateless protocol. Every time you post to a web server, it has no intrinsic recollection of previous pages or conversations with the client. So, there is clearly something going on here which enables the page to remember what is in this control from the previous iteration so that it can add to it on the next iteration. This mechanism is called View State and it is implemented quite seemlessly actually by asp.net but it is quite important for you to understand exactly how View State works. So, I’m going to spend the next few minutes just looking at the view state mechanism and see on how it fits in to the page life cycle. So, we close the page here and take a look back in our default.aspx page and consider this mechanism now.
Okay, so here is a slightly simplified summary of what happens the first time you request the web page from asp.net. There are six steps that we are going to look at here. I am going to leave out a few page events which will come back too later but at the moment in this stage of my discussion, there are six steps for you to consider. To illustrate these points, we are going to run the application in the debugger and we are going to set break points only to the methods to see what is happening.
So, let us switch to the code view. And let us set some break points in our codes. So let us go to the page load method and set a break point on the first statement, okay. And also let us go down to our Event Handler and set a break point in their as well. Take it. Let us run the application in the debugger. Start debugging. So, this is the usual dialogue box that pops up with me to just click OK here to enable debugging in all web application. Okay, so it hits the first breakpoint and it shows us that it has involved the page load method to initialize the content of our web page. This is actually a little bit misleading because there are some initialization that has already happened before we get a chance to intervene. So, let me just explain exactly what is going on. When we visited the web page, asp.net created an instance of our web page class and it is called default. And it created child controls inside my default class for each runat server that I have in my web page. So, for example if I view in design mode, we have a label and we have a text box and list box and a few buttons and labels and so on. Each of this runat server controls would be represented by a .net object which is a member of this class.
Furthermore, this default values will be set according to the mark up that asp.net sees in our web page. So, for example, here we have a label, Welcome label and it has a text property and the text property specifies some default initialization text for our control. So, that is the first place where initialization takes place. Asp.net will create a label object and it will call the label object, “Welcome Label” and it will set the initial value to be “Enter your name and travel details for this year” and all of that happens before we get into the page load method. And to prove that, if I just put my cursor there and right click. If we are going to quick watch, I am going to watch for the expression, “Welcome Label”. And then you see that it already contains the text, Enter your name, etc, etc as per the static mark up in the aspx page. All of that has happened before we have been to the page load method.
So clearly, if you have static initialization, the best place to put it is in the mark up page. Nevertheless, situations do arise where you have to perform programmatic initialization and that is why we have the page load method of course. So in here, on the first visit, we set the Country list and then on every visit, we set the time stamp label. So, if I just step over some of these statements. And I’ll just stop there before going any further.
At this point, we have reached the end of the page load method and that concludes the initialization phase for the page object. Af
Transcription by:
Scribe4you Transcription Services