PC Review


Reply
Thread Tools Rate Thread

ASPNET 2.0 Page_Load question

 
 
Jason Huang
Guest
Posts: n/a
 
      21st Sep 2007
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


 
Reply With Quote
 
 
 
 
Just Me
Guest
Posts: n/a
 
      21st Sep 2007
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.



"Jason Huang" <(E-Mail Removed)> wrote in message
news:utIi$bB$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      21st Sep 2007
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/
======================================
"Jason Huang" <(E-Mail Removed)> wrote in message news:utIi$bB$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      21st Sep 2007
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



"Jason Huang" wrote:

> 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
>
>
>

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      21st Sep 2007
"Jason Huang" <(E-Mail Removed)> wrote in message
news:utIi$bB$(E-Mail Removed)...

> 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
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Shelly
Guest
Posts: n/a
 
      23rd Sep 2007

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:ue6aYYE$(E-Mail Removed)...
> "Jason Huang" <(E-Mail Removed)> wrote in message
> news:utIi$bB$(E-Mail Removed)...
>
>> 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
> }
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Materpage page_load event fired after content page_load? rockdale Microsoft ASP .NET 1 16th Nov 2006 06:59 PM
beginner question about page_load Michelle Microsoft ADO .NET 1 3rd Oct 2005 06:54 PM
Page_Load question Andrea Williams Microsoft ASP .NET 5 8th Mar 2005 03:12 AM
Why does page_load fire twice when inheriting from a common overridable Page_Load bminder Microsoft ASP .NET 1 23rd Feb 2004 08:54 PM
Re: Page_Load question msnews.microsoft.com Microsoft ASP .NET 2 14th Aug 2003 06:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:44 PM.