Reputation: 15389
I've may have misunderstood how AJAX works - Can someone shed some light on the following simplified scenario:
I have a asp.net web application. On a page there is a user control inside an update panel (with no properties changed) and a script manager.
In the user control and on the form there is the a label, both get their text set to DateTime.Now.ToString
in the load event. There is also a button, which causes a post back in the user control.
When I click the button, as I expect the label inside the user control updates and the one label on the page does now. So far so good.
However... the page load event on the page does get processed with isPostBack = True (which I did not expect), and it looks like whatever happens in the load event doesn't get pushed back to the client (as the label didn't update).
I didn't expect the page load event (in the page that contains the user control) to be raised and processed when an AJAX panel is updated, is this correct? or am I doing something wrong? I remember reading something about Page.IsCallback, but that is false, so maybe that has nothing to do with this.
Upvotes: 7
Views: 1016
Reputation: 18832
During a PostBack all the page life cycle events get executed again and the page is recreated and resent to the client.
An UpdatePanel essentially does a full postback but only the controls inside the update panel are refreshed on the client. So any controls outside the update panel will not update even though you might change their value on the server.
You can use the Page.IsPostBack
property to check whether you doing the initial loading of the page or a postback. eg to only do something the first time the page loads:
if (!Page.IsPostBack)
{
//doSomething
}
Upvotes: 0
Reputation: 22297
Well, this question is isn't about AJAX per-se, but about Microsoft's AJAX-based UpdatePanel, which is a complex beast. The simple explanation of the way UpdatePanel's work is that everything works the same as a normal full-page "post back" (ViewState POSTed to the server, server-side DOM is recreated, all of the page event life-cycle events are executed) except at the very end the response rendered to the client only includes the subset of HTML needed to refresh the content of the UpdatePanel from which the AJAX request was initiated. There are some additional subtleties and complexities at play, but this is the basic idea.
Upvotes: 6
Reputation: 2327
Without seeing some samples of what you're doing, I don't think it's possible to say if you're doing something wrong.
But, yes, Update panels still invoke most of the page life cycle on the server side. If what you're looking for is to avoid the page lifecycle, page methods may be more to your taste. They do require mucking around with javascript though.
http://forums.asp.net/p/1070297/1571597.aspx
Upvotes: 1
Reputation: 3775
All of the page lifecycle events get executed even on partial postbacks. You can differentiate between a full postback and a partial postback by doing the following:
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
}
Upvotes: 2