C# – ASP.Net: Page_Load() being called multiple times

asp.netc

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.

Best Answer

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.