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
Just Me - 21 Sep 2007 07:24 GMT
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.
> Hi,
>
[quoted text clipped - 8 lines]
>
> Jason
Juan T. Llibre - 21 Sep 2007 07:46 GMT
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" ?
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
> Hi,
>
[quoted text clipped - 6 lines]
>
> Jason
Peter Bromberg [C# MVP] - 21 Sep 2007 12:24 GMT
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
> Hi,
>
[quoted text clipped - 8 lines]
>
> Jason
Mark Rae [MVP] - 21 Sep 2007 12:53 GMT
> 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
}

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
Shelly - 23 Sep 2007 21:50 GMT
>> I found whenever I click the Button control Btn1, then the Page_Load
>> event will be called first.
[quoted text clipped - 11 lines]
> // 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