Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net: Page_Load() being called multiple times

Tags:

c#

asp.net

I don't know alot about ASP.Net but I'm trying to make a new control for a message box. You enter some info and press a button.

However, for some bizarre reason when the button is pressed, Page_Load() gets called a second time, and all of the member variables are reset to null! I need those variables, and Page_Load() has not reason to be called a second time! Of course the callstack is useless.

like image 440
evilfred Avatar asked Jul 07 '09 21:07

evilfred


People also ask

What is the difference between Page_Load and Page_Init?

Note that the Page_Init event fires only the first time the page is loaded. When you use a web form and post back to this page again, the Page_Init event doesn't fire. But the Page_Load event fires each time the page loads.

What is Page_Load method in asp net?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

Which page loads first master or content?

Content page controls PreRender event. Master page controls Unload event. Content page controls Unload event.

What is Pageload in C#?

Page_Load() method is called after a preLoad event. With Page_Load() you can set default values or check for postBacks etc. protected void Page_Load(object sender, EventArgs e) { int x = 10; } write this and put a break-point on int x = 10; watch sender and e.


3 Answers

Remember, in ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entire page life cycle, including your page load code, and not just the click code.

Always two there are, no more, no less. A request and a response.

like image 157
Joel Coehoorn Avatar answered Oct 17 '22 18:10

Joel Coehoorn


When the page posts back, the Page_Load method is called. Then, once the server actually processes the page and sends you a new one based on changes, the Page_Load is called again, actually the first time on the new page sent to you.

So if you are pulling data in the Page_Load event or setting some values, enclose it in the following block:

if(!Page.IsPostBack)
{

}

to preserve some of your state. Otherwise, the instructions that you put into the Page_Load event will execute every time.

It helps to review the ASP.Net page lifecycle :)

like image 5
Eugene Avatar answered Oct 17 '22 18:10

Eugene


As Joel mentioned, instance variables will be lost once the page is sent back to the client. However, there are various methods of storing the values of your variables so you can retrieve them later. This page on State Management is a good starting point if you want to learn more.

like image 1
tbreffni Avatar answered Oct 17 '22 18:10

tbreffni