8

I have page with some asp.NET form stuff on it, including drop down lists and a calendar. I had trouble with stuff being NULL, so I tried putting in a breakpoint in my constructor. It stopped at the breakpoint before the page first loaded as it should. Then I stepped past it and the page loaded. I then chose a date in the calendar and it stopped at my breakpoint. I don't use the event of date chosen for anything. Actually, my page only uses the event when the button on it is clicked. But somehow, it looks like my constructor is called at all events.

Is this true? How can this be true? Does it make an entirely new instance of the page at each event fired?

5
  • 5
    Show your code. Also, if this is ASP.NET MVC tag it accordingly. Aug 15, 2014 at 13:33
  • 1
    Show some code please. All we can do is guess what the problem is when you don't show code - and that isn't helpful for anyone. Aug 15, 2014 at 13:35
  • 2
    From what you're describing it sounds like you're using ASP.NET, if this is right I believe you're talking about the code behind page rather than a controller. A controller is term in MVC (a different .NET web framework)
    – Liath
    Aug 15, 2014 at 13:52
  • I have edited the question to remove the word "controller". As @Liath says, this is a specific term that is only meaningful within MVC and is confusing when you're talking about standard ASP.NET. (Thanks, Liath - missed one. :) ) Aug 15, 2014 at 13:58
  • @GalacticCowboy, there's nothing like "standard asp.net".. There's webforms and MVC. Both coexist in the same asp.net space. Both are standard asp.net approaches...
    – walther
    Aug 15, 2014 at 23:21

1 Answer 1

14

Yes, it is normal for a new instance of your page to be created for all postbacks. The alternative would be to keep the server-side object around for as long as the user's session persists, even though the server has no way of knowing whether the user is even still looking at the page. Since that is undesirable, an alternative approach is needed: pages get destroyed and re-created between each request.

Some controls, including indeed calendar controls, will automatically cause postbacks, in order to (e.g.) show the proper days of the month after the user has selected a different month. Other controls don't cause such postbacks, either because they don't need new server-generated information at all, or because they use other methods to contact the server. The control's documentation should tell you so, and how (if at all) that behaviour can be controlled.

9
  • In steps ajax with it's partial page functionality.
    – BLoB
    Aug 15, 2014 at 13:39
  • Also one can define static methods on Page object (and marking them with WebMethod attribute), making them available for AJAX calls, those do not instantiate new Page object (without actual asp.net PostBack) and thus only trigger static constructor if it has not been called yet.
    – vittore
    Aug 15, 2014 at 13:43
  • 6
    @PaulZahra I'm not sure I understand your comment, but anything that uses an UpdatePanel control, whether directly or behind-the-scenes, actually creates a new page on the server, just like a traditional postback would. It's only in how the updated page is sent to the client that things change: only the specified controls' rendered output gets sent back.
    – user743382
    Aug 15, 2014 at 13:43
  • @vittore Sure, that's possible, but that's different, that doesn't count as a postback in ASP.NET terms. It may count as a postback in how you personally, others, and possibly even I would refer to it, but in ASP.NET terms, it's not. :)
    – user743382
    Aug 15, 2014 at 13:45
  • 1
    @GalacticCowboy I think in order to properly answer this question big picture of asp.net webforms page lifecycle should be referenced, as there are a lot of players here. ( msdn.microsoft.com/en-us/library/aa479007.aspx )
    – vittore
    Aug 15, 2014 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.