Async. Postbacks cause Page_Init? (C#)
问题
I'm experiencing a very strange problem...
I have a regular ASP.Net webpage with a page_init and a page_load function. It is my understanding(from everywhere I look) that page_init gets called on the first page load(as in, not ever called in a postback) and page_load is called anytime something happens with the page.(It is very hard finding any info about this except for dead links and stuff about the page life cycle)
Well, I have an update panel containing other update panels and other assorted controls. Anytime I edit one of these controls, an async postback happens but instead of only page_load being called, page_init is also called which isn't suppose to happen(and didn't happen before a big codebase change)
So I would like to know anything that might cause this behavior or just if my idea of how page events are called is wrong.
回答1
I think you have the wrong idea of the page load life cycle. The OnInit event is called on EVERY request. Having the Page_Init method in your code behind is a shorthand way of wiring up the pages OnInit event.
Now I believe that you are confusing this with the "IsPostBack" property which will be set to true if a page posts back to itself i.e. when you click a Button etc. My guess is what you need to do is add an if statement in your Page_Init method i.e.
if(!IsPostBack){
//Do something to to update the UI
}
回答2
Page_Init
is definitely called on every page hit, postback or not, exactly the same as Page_Load
.
The misconception that Page_Init
doesn't get called on every request seems to be a common one.
Are you certain that this wasn't happening before your "big codebase change"?
回答3
Your Page_Init as well as Page_Load both methods should be called each time.