Have I completely misunderstood ASP.Net AJAX (Update Panel)?
Asked Answered
C

4

7

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.

Chicle answered 26/11, 2010 at 16:15 Comment(8)
I think the ajax control toolkit is a complete mess and the javascript code that gets generated is really bad. I would recommend that you instead learn javascript and ajax how it should be done and maybe use a javascript framework like jquery to solve your problems.Pressmark
@Tomas, while I agree that the Ajax Control Toolkit is a mess, I think that if you are still working within the traditional ASP.NET framework UpdatePanels are still the way to go the majority of the time. But you want to learn how to be very selective about which components have ViewState enabled with the default being false. That being said, the whole traditional ASP.NET paradigm is a mistake and I would suggest an MVC framework like ASP.NET MVC in combination with a command of Javascript and JQuery.Bat
@Stephen: You can make sort of "ok" stuff in the old webforms if you avoid UpdatePanels. You could define services that returs json and make call against those with jquery instead of doing regular postbacks. I've only been in one project using UpdatePanels and I'm glad I have since it opened up my eyes in how I should not solve a similar problem.Pressmark
Originally I'm a win forms / web service dev, and I think ASP.Net has put me off Web Development for Life... glad to hear there are alternatives.Chicle
@Tomas, I suppose you're right, what you are describing I've done too for really complex pages with success, it's a kind-of MVC emulation. The philosophy behind my recommendation is a soft-of "When in Rome...".Bat
@Mr Shoubs: yes, ASP.NET is very hard too learn and filled with all kinds of frustrations. It sets you up for failure and it takes a really long time to learn how to out maneuver it. Personally, I haven't worked on a traditional ASP.NET app in 1.5 years, and I'm not looking back. Since then, I've dabbled with ASP.NET MVC and found it promising. I've maintained Java Spring / Structs MVC apps and found them even more horrible. For the past 6 months I've worked a new dev. project in Grails and found it not perfect but good and the best web framework I've used yet.Bat
@Stephen: Interesting to know. We have a very small team, so MVC always seemed a bit overkill. And getting everyone to 'convert' to some other technology is always a problem when there isn't any experience in house... guess it's times like these when you need to start hiring in experience.Chicle
@Mr Shoubs: glad to share my experiences. MVC is actually very simple, but it requires you to have an intimate knowledge of http, html, and javascript. But I've not seen any successful attempts at abstracting these away, it always leads to misery.Bat
B
6

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.

Bat answered 26/11, 2010 at 16:21 Comment(0)
C
2

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)
   {

   }
Cowans answered 26/11, 2010 at 16:24 Comment(1)
You rock!! much appreciatedUtterance
H
1

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

Hulk answered 26/11, 2010 at 16:29 Comment(0)
D
0

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
}
Debrahdebrecen answered 26/11, 2010 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.