Page_Load executes before aspx is rendered?

G

gnewsgroup

This might have been discussed before, but I haven't been able to dig
it out.

I ask because I notice something interesting.

Suppose, I have the following controls in a web form.

<asp:DropDownList ID="DropDownList1" runat="server" > </
asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Suppose, in Page_Load, I want to do:

Label1.Text = DropDownList1.SelectedItem.Text;

If I bind the DropDownList right in the aspx file through
SqlDataSource, Label1.Text is empty when I check out the application
in the browser.

If I bind the DropDownList in the code behind, and in Page_Load, I do:

BindTheDropDownList();
Label1.Text = DropDownList1.SelectedItem.Text;

Label1.Text does have the text value of the 0th item of the
DropDownList.

Therefore, I think that at Page_Load, the binding processes in the
ASPX file are not done yet. Am I right?

Thanks.
 
G

gnewsgroup

Yes. The binding of this sort takes place in the PreRender event.

Thank you. I think by "binding of this sort", you are referring to
the kind of binding in aspx file through SqlDataSource?
 
C

clintonG

I copied these notes out of a base class I had handy...

// Events in ASP.NET Master and Content Pages
// http://msdn2.microsoft.com/en-us/library/dct97kc3.aspx

//1.) Master page controls Init event.
//2.) Content controls Init event.
//3.) Master page Init event.
//4.) Content page Init event.
//5.) Content page Load event.
//6.) Master page Load event.
//7.) Content controls Load event.
//8.) Content page PreRender event.
//9.) Master page PreRender event.
//10.) Master page controls PreRender event.
//11.) Content controls PreRender event.

//http://www.dotnetspider.com/qa/Question27787.aspx
//Page_Init (VB) OnInit (C#)
//This is the first step in a Web form’s life cycle.
//The server controls are loaded and initialized from the Web form’s view
state.


//Page_Load
//The server controls are loaded in the Page object. View state information
is available
// at this point, so this is where we may put code to change control
settings or display
// text on the page.

//Page_PreRender
//The page object has been compiled and is about to be rendered.

//Page_Unload
//The page is unloaded from memory.

//Page_Disposed
//The Page object is released from memory. This is the last event in the
life of a Page object.

//Page_Error
//An unhandled exception occurs.

//Page_AbortTransaction
//A transaction is aborted.

//Page_CommitTransaction
//A transaction is accepted.

//Page_DataBinding
//A server control on the page binds to a data source.


Eliyahu Goldin said:
Yes.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Yes. The binding of this sort takes place in the PreRender event.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP
[ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

Thank you. I think by "binding of this sort", you are referring to
the kind of binding in aspx file through SqlDataSource?
 
G

gnewsgroup

I copied these notes out of a base class I had handy...

// Events in ASP.NET Master and Content Pages
//http://msdn2.microsoft.com/en-us/library/dct97kc3.aspx

//1.) Master page controls Init event.
//2.) Content controls Init event.
//3.) Master page Init event.
//4.) Content page Init event.
//5.) Content page Load event.
//6.) Master page Load event.
//7.) Content controls Load event.
//8.) Content page PreRender event.
//9.) Master page PreRender event.
//10.) Master page controls PreRender event.
//11.) Content controls PreRender event.

//http://www.dotnetspider.com/qa/Question27787.aspx
//Page_Init (VB) OnInit (C#)
//This is the first step in a Web form's life cycle.
//The server controls are loaded and initialized from the Web form's view
state.

//Page_Load
//The server controls are loaded in the Page object. View state information
is available
// at this point, so this is where we may put code to change control
settings or display
// text on the page.

//Page_PreRender
//The page object has been compiled and is about to be rendered.

//Page_Unload
//The page is unloaded from memory.

//Page_Disposed
//The Page object is released from memory. This is the last event in the
life of a Page object.

//Page_Error
//An unhandled exception occurs.

//Page_AbortTransaction
//A transaction is aborted.

//Page_CommitTransaction
//A transaction is accepted.

//Page_DataBinding
//A server control on the page binds to a data source.

Nice documentation, now, that's clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top