ASPNET 2.0 Page_Load question

J

Jason Huang

Hi,

In my ASPNet 2.0 C# web application Form1.aspx, there are 1 TextBox control
txtBox1, 1 Button control Btn1, and 1 GridView control GV1.
I found whenever I click the Button control Btn1, then the Page_Load event
will be called first.
This also applys to the situation when I check a CheckBox in the GV1.
I am wondering why in the aspx web form, whenever we click any controls,
they all call the Page_Load event?
Thanks for help.


Jason
 
J

Just Me

The page load will allways fire before any of the the events caused by
button pushes etc. It is a programatic requirement to be able to do things
at this time before other events load.
 
J

Juan T. Llibre

Hi, Jason.

re:
!> I found whenever I click the Button control Btn1, then the Page_Load event will be called first.

That's because you're POSTing the page when you click the button.

Please review the ASP.NET Page Lifecycle ...

http://msdn2.microsoft.com/en-us/library/ms178472(VS.80).aspx

re:
!> I am wondering why in the aspx web form, whenever we click any controls, they all call the Page_Load event?

Do you have autopostback set to "true" ?
 
G

Guest

The default behavior of a button control is to generate a postback. Other
controls can be set to generate a postback with their events (such as
TextChanged for textbox). A postback is nothing more than a form posting to
itself.

Page_Load always runs when a page is loaded, whether it is a postback or
not. Then, for any other events such as a button click, their event handler
code is run.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
 
M

Mark Rae [MVP]

I found whenever I click the Button control Btn1, then the Page_Load event
will be called first.

In addition to the other responses, you can use the IsPostBack property of
the Page object to determine whether your code within the Page_xxx events
runs or not...

if (!IsPostBack)
{
// run when the page first loads
}
else
{
// run when the page posts back to itself
}
 
S

Shelly

Mark Rae said:
In addition to the other responses, you can use the IsPostBack property of
the Page object to determine whether your code within the Page_xxx events
runs or not...

if (!IsPostBack)
{
// run when the page first loads
}
else
{
// run when the page posts back to itself
}

Once more you have taught me something Mark. I just encountered this as
well and had to devise code to make things work as I wanted. I had a page
that could be invoked from another page and pass nothing, or from a url
click that involved passing four variables. This drove me crazy until I
figured out that the page load occurred first. Having IsPostBack would have
solved my problem in a hardened manner, rather than the kluge I put
together. Thanks.

Shelly
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top