When a button on my web form is pressed, code execution calls the page_load method first. WHY?

 

Hello. I am working on a C#/ASP.NET web application. What I want to accomplish is the following:

1. When a page named "Qwan.aspx" is loaded, then set a global variable "myVar" in the Page_Load method for the code behind page Qwan.cs.

2. Display the web form Qwan.aspx on the screen.

3. A user presses a button on the web page Qwan.aspx.

4. Step 3 causes the button event handler method to be called.

The problem here is that when step 3 is performed, code execution again goes to the page_load method of the Qwan.cs file. Why is this? Shouldn't code execution go directly to my event handler(Method) for the button control?

0
AppDevForMe
1/15/2007 8:43:07 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
1434 Views

Similar Articles

[PageSpeed] 35

Your thinking is like WinForms developer, but in ASP.NET it is a little bit different. ASP.NET uses communication in style request - response. Browser creates requests and server sends response. When is request to server send, ASP.NET executes page and also complete page lifecycle. Page_Load method is part of page lifecycle, this method is called at each request. Button event handler is postback event handler, it executes only when button is clicked. Try tu study somethink about ASP.NET page lifecycle and about postback.

0
Marian
1/15/2007 8:49:21 PM

thats pretty much what happens. one thing you can do is in your page_load method is add a

 

if (!ispostback){

 //Your code goes here

}

 also i believe you may be interesting in looking up the functionality of the autopostback attribute. i think that might also solve your problem.

GOODLUCK! 

0
tont
1/15/2007 8:53:03 PM

Before the code connected to your button can execute, the page must be loaded from disk.

 

Stage Page Event Overridable method
Page initialization Init  
View state loading   LoadViewState
Postback data processing   LoadPostData method in any control that implements the IPostBackDataHandler interface
Page loading Load  
Postback change notification   RaisePostDataChangedEvent method in any control that implements the IPostBackDataHandler interface
Postback event handling Any postback event defined by controls RaisePostBackEvent method in any control that implements the IPostBackEventHandler interface
Page pre-rendering phase PreRender  
View state saving   SaveViewState
Page rendering   Render
Page unloading Unload

 I pasted the above from this web site:

http://msdn2.microsoft.com/en-us/library/aa479007.aspx


Steve Wellens

My blog
0
SGWellens
1/15/2007 8:55:06 PM

i recommend you to read the link below which has the best informative information about the page life cycle in order to understand why the page_load event was raised in step 3

http://www.15seconds.com/issue/020102.htm

Happy Coding


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
1/15/2007 8:56:23 PM
Reply:

Similar Artilces:

calling a web form from another web form
I would like to know what is the best way to open a web form from another web form. I would like to call it from a button control. Thanks Well, if I've grasped your question right, there are many methods to do that: 1)Response.Redirect > To navigate to another page from code and is equal to clicking a hyperlink. I suggest this as I think it's closer to your question. 2)Server.Transfer > This ends the current web form and begins a new web form. It's usable when you want to pass some variables i.e (Label1.Text) to the next immediate page. It's not safe though and not used to send p...

Web Form : Block the return button on the web form
Hello, I have a principal form with a list of information. From this form, I go on an edit forms and I want to control what is done on this form. For this, I want to block the button Windows back on my edit form. Somebody can help me ? Thank you for your help.  Jy. set the Session Variable (with your ID or whatever ) in the Principal Form when user click on Edit then put the One Back Button on your edit form and Redirect the page by using the Session variable value.Please Mark as Answred If This Blog has Helped You.RegardsMitesh Darjihttp://www.indianic.comhttp://www.mitatdotnet....

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

how to change these windows form code to web form code?
hi, i'm a beginner of c#,i want to know how to change these windows form code to web form code //////////// private void buttonSign_Click( object sender, System.EventArgs e) { //get original message as byte array byte[] messagebytes = Encoding.UTF8.GetBytes( textOriginalMessage.Text); //create digest of original message using SHA1 SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] hashbytes = sha1.ComputeHash(messagebytes); //display hash bytes in hex format StringBuilder sb = new StringBuilder(); for (...

For interrupting.web form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form
Web Form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form ! Because of my pool English, if you can't see , I will explain more ! ThanksGambler is the real life I guess you mean when form grows it also grow parent div element. This is normal. If you want div to stay fixed size and rather clip the form and maybe show a scrollbar, look at overflow property in CSS.Thanks------------------------------------------------------------This posting is provided "AS IS" with no warranties, and confers no ...

Web Form vs. Web Content Form
If you don't mind, I would like to know what is the difference between those two items, when I go to add a new item I see both of those and I don't know what the difference is.  A content form is for use with a masterpage. A web form is a standalone web page. If you select web content form, you will then have to specify a masterpage for it. and what is a master page used for?  Hi, a master page is used to give a constant look and theme to a site, without having to duplicate code. So basically you can put your header, menu, nav and footer on a master page. Then a...

How to fire events in other web form from other web form ?
Suppose that I have A and B web form. I want to trigger B web form's Page_Load event from A web form. How can I do that ? Thanks in advanced WilbertWilbert Bujn Create a public method in B that call the load method : public void Load(object sender, System.EventArgs e) { Page_Load(sender, e) ; } In the page_load of A, instanciate a B object and then call the function B myB = new B() ; myB.Load() ; Quicky Thank you quicky, it works fine. WilbertWilbert Bujn...

Include web form inside web form
I tried to include an aspx file in a asp.net web form. However, all the web controls (textbox, button) in the included page are not displayed when called. <Form id="Form1" method="post" runat="server"> <%   Response.writeFile("subPage.aspx"); %> <asp:button id="btnPrev" runat="server" text="Previous"></asp:button> </Form> There are some <asp:textbox> controls in the subPage.aspx file, the file itself works fine, but when it is included in the main page, all the controls are not displayed. Please assist. Thanks   u should u...

Web Form embedded in another Web Form
Hi,  I have programmed a Web Form which acts like a scientific calculator. I would like to embedd this calculator in the upper left hand corner of another accounting Web Form. I don't want to host the calculator on the same server as the accounting page. I.e. I want them to have distinct URLs. Classically, I would do this using frames, but is there a better way using ASP? Is there some sort of Panel which can display the webpage at a specific URL which I can just drag onto my accounting Web Form? Thanks,  P1000   Frames are the only way to embed a webform inside ano...

Why a web form button click causes Page_Load to execute?
I am noticing that a web form button 'GoToPage2' when clicked does not immediately go to the click event of this button; rather, on clicking the button, the Page_Load sub-routine of the page which hosts the button is first executed, and only after this, the GoToPage2_Click sub-routine executes. This, I think, would degrade performance if all that the button is doing is transferring user to another page after checking for user credentials. CAN SOME ONE PLEASE EXPLAIN AS TO WHY THIS IS HAPPENING AND ALSO, IF THERE IS A WAY TO HAVE A BUTTON THAT DOES NOT BEHAVE IN THE ABOVE MANNER? Tha...

How do you call a windows form from a web form?
hi,  i doing a vb project which include a web form and a windows form.   How do you call a windows form from a web form? can any kind souls plz tell mi haha....my email is EDITED. i appreciate the help   regards kit  EDITED By: longhorn2005 This gives you an idea using VS 2003-- most of it is still relevant:  http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/---------------------------------------MCP - Web Based Client Development .NET 2.0 I am attempting to do something similar, I have an existing windows form application that I was asked to i...

call a web form from a window form
Hi all I need help about calling a web form from a window form. i don't know if this is possible but if it possible please give me some help, any kind of help. thanks. Miguel A. Usually you can open any exe from within the windows form, and to do so, you could open the iexplorer.exe and give it the url of the webform to run it. regards.Bilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP would you give me an example? please, tanksMiguel A. please i need some documentation or examples about this.Miguel A....

Web resources about - When a button on my web form is pressed, code execution calls the page_load method first. WHY? - asp.net.web-forms

Execution of Joseph Wood - Wikipedia, the free encyclopedia
Wood was convicted of two counts of first degree murder and two counts of aggravated assault against a police officer. He was sentenced to death ...

Cie Games Founder Justin Choi: Execution Key To Success On Facebook’s Gaming Platform
... its flagship game to continued success and some 5.9 million monthly average users . The key, according to Founder Justin Choi: Better execution. ...

SAP Retail Execution on the App Store
Read reviews, compare customer ratings, see screenshots, and learn more about SAP Retail Execution. Download SAP Retail Execution and enjoy it ...

Execution Bed - Flickr - Photo Sharing!
... the microphone; it is why I am here. There are few acts graver than when a government takes the life of one of its own citizens. Executions ...

(Raw Video) Taliban Execution of Two Women in Afghanistan - YouTube
PLEASE THIS IS VERY serious WARNING This video may be disturbing to many and what you are about to see may be hurtful to watch this story is ...

Executions of Afghans reviving resentment of Iran
Many Afghan refugees hope for a haven in their western neighbour but return with stories of abuse, torture and addiction

Pasquale Barbaro: Sydney figure evaded hitman execution in Leichhardt - DailyTelegraph Search Search
PASQUALE Barbaro is a well-known man around Sydney, with a family pedigree littered with dead bodies.

Liveblogging History: November 19, 1915: Execution of Joe Hill
**Wikipedia**: [Joe Hill](https://en.wikipedia.org/wiki/Joe_Hill): >Joe Hill, born Joel Emmanuel Hägglund in Gävle, Sweden, and also known as ...

Open thread for night owls: GOP candidates attend event led by man who calls for execution of gays
Kevin Swanson is a far-right pastor and radio host who organized last weekend’s Iowa “National Religious Liberties Conference”, attended by Republican ...

Supreme Court stays execution of Missouri inmate
Supreme Court stays execution of Missouri inmate

Resources last updated: 11/23/2015 5:30:31 PM