Link to home
Start Free TrialLog in
Avatar of x666xchange
x666xchange

asked on

What exactly happens when a Page_Load event is triggered in ASP.NET ?

In ASP.NET you code for the Page_Load sub insite a <SCRIPT> block whose "runat" property is set to 'server'.

This means that the <SCRIPT> block will be processed at the server end. But the Page_Load event is fired when the ASP.NET page loads into the browser, so that would be a  <<< client-side event >>, if I've understood that correctly.

So if I have for eg. a label control on my page to which I set some value in the Page_Load sub, then who exactly is setting the value of the label control ? The client or the server.

If it is the server then my query is, does the client notify the server in some way when the Page_Load event has occurred at the client end and consequently, the server during the page loading process itself, sets the value of the label.

On the otherhand, if it were the client that sets the value of label control during page load, how is that possible ? Coz once the page is sent to the clien by the server, that's the end of story. Meaning one full cycle of client-server interaction is over and the server will won't do anything until the next request is initiated from the cleint end or the browser.

So what exactly is the mechanism at work ?

The confusion in my mind is more with respect the text I have bracketed in angle brackets above. So who exactly does the job of setting the value of the label control and how ?
Avatar of naveenkohli
naveenkohli

The processing occurs on server side. Its the .Net framework on server side that is setting the values of the controls.
For example
<asp:Label id="myLabel" Runat=server Text="Foo"></asp:Label>

When server side processes the page, it creates an instance of asp:Label class object. All the controls have some common properties like ID, Text etc. It assigns the values to these properties that you specified in the tag itself. After all the properties have been set, the framework calls Render method on each control. In response to each render call, the controls emit the corresponding HTML tag. For example, incase of Label control, it will emit <span> tag. And on post backs, the values of controls are restored from VIEWSTATE unless u have disabled ViewState
Avatar of x666xchange

ASKER

Dear Naveen,

Well first of thank you very much for the very prompt reply. However, I request you to kindly re-consider the first 2 paras of my query. Especially the 2nd one that has the angle bracketed text. I hope those paras would clarify to you as to where my confusion lies. THANK YOU :-)
See the Page_Load event occurs when the page is loaded into the browser. That means at the client-end. So if I set the value of a particular label-control in the the Page_Load sub to say "ABC", is this statement actually occuring at the client side adn not at the server side. Or during Page_Load itself does the client meanwhile send a notification to the server which then does the setting of the value of the label.
lets say my script block is this way

<script language="VB" runat="server>
 sub Page_Load(obj as Object, E as EventArgs)
    << some_label >>.text = "XYZ"
 end sub
</script>
lets say my script block is this way

<script language="VB" runat="server>
 sub Page_Load(obj as Object, E as EventArgs)
    << some_label >>.text = "XYZ"
 end sub
</script>
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Naveen,

I wasn't able to check in detail ur most recent comment in detail when it appeared on this page on Friday coz I had checked my intimation main on Friday night.

But then after having gone through it much later in the evening ( that bought me time to once again go through my reference materials a bit more closely ), on Saturday I can say that in a way I had a similar hunch about when Page_Load occurs as has been put by you in ur comment in the following words: "loading on controls in ASP.Net occurs << before the page is actually loaded on client side >>. As I mentioned earlier, Page_Load is event fired by framework ".

Of particular interest to me are your words that I have bracketed in angle-brackets. I guess that was the hunch I had and the area that I was looking for re-confirmation from ur end. I feel relieved now that I was thinking in the right direction, thought I wasn't 100% sure. The confusion was because the reference material stated that the Page_Load even is fired when the page is loaded in the browser. Mark words "loaded in the browser". That's what really stumped me.

Thank you very much for your very valuable insight, help and patience. I greatly respect your knowledge and experience.

In the end, I request you to kindly have a look to a more recent query I posted on the concept of VIEWSTATE itself. Not the one that u had responded to, prior to that one title "ASP.NET - Query on ViewState".

Please check my query titled "ASP.NET - Query on ViewState"