Postbacks or Not?

Some thoughts on whether or not to use postbacks in authoring an ASP.NET Web application, what are some of the side-effects, and what we're doing in Whidbey to improve the postback model...
I had an interesting conversation with a couple of TechEd attendees on the subject of whether or not to use postbacks in a Web application.

At the core, the issue was that postbacks degrade the end-user experience: the back button doesn't always make sense, the resulting flicker, the loss of scroll position and focus. All real problems. These folks I talked to had completely replaced postback usage with their own client-script framework they invented on ASP.NET v1.1 to send data down to a Web service hosted on the server, and update the UI based on the results.

Postbacks seem more natural for things like simple data entry forms, search engines - things that HTML and the current generation of browsers were designed for. But postbacks are infact truly the wrong thing for expanding a tree node in a dynamic tree, or navigating rows in a grid. I do see the appeal of the non-postback world. You navigate to an application, and from there on the UI comes and goes, or rather changes with smooth transitions. No more flicker, or unexpected enabling of the back button, simply because you clicked a Submit button.

<tangent> Thinking a bit about navigation, and the address bar. What if the address bar continued to indicate "foo.com" regardless of hyperlinking around to a different section of the current screen, or some other physical screen. Its all the same logical app, "foo.com," anyway. Essentially the server sends down an "app" whose UI morphs as you postback, or navigate. Curious to hear the opinions and thoughts on the feasibility of this model. Should Web applications head down this route? </tangent>

Postbacks happen to be a core aspect of the Web Forms framework. Both the lifecycle of a form and the associated event-based programming model revolve around postbacks. In general, things work better when you go along with the flow of the underlying of framework. To that end, we're doing various things in Whidbey in the Web Forms framework to improve the postback model.
  • We can maintain scroll position.
  • We can maintain which control had focus, and restore focus, or allow server code to focus a new control (coupled with our validators now being intelligent enough to automatically set focus on the control in error).
  • Introducing a new feature called "Callbacks" that can be used by server control developers. Essentially this allows a control to asynchronously fetch data without causing postbacks, which allows controls to be smart about when they use postbacks. More on this in an upcoming post.
Posted on Tuesday, 5/25/2004 @ 12:03 PM | #ASP.NET


Comments

21 comments have been posted.

Dylan Greene

Posted on 5/25/2004 @ 12:25 PM
While I 100% agree with everything you wrote, I use the technology today in my work (via XMLHTTP), there are still problems because of the browser model:
1) The Back button no longer works as expected.
2) Bookmarking no longer works as expected.
3) Refresh no longer works as expected.

Until a web application designer can address these problems via improvments to the web browser, do you see these as issues as "not important", or are they being addressed?

RichB

Posted on 5/25/2004 @ 3:31 PM
Postbacks are great for small, intranet-like sites because of the ease of development. They are poor for external, large sites. Of all the reasons for this, the biggest has to be Bookmarks.

Imagine if Google's results page was a PostBack page. The beauty of a site like Google's is you can send a link like http://a9.com/nikhil to someone and see a list of items about a subject.

Brian Broom

Posted on 5/25/2004 @ 3:43 PM
I think the best answer may be in the middle. There are things that postbacks make sense for, and things they don't. The "entire website as one item" has problems mentioned above (back, bookmark, refresh) not to mention page load time.

The changes you mention for Whidbey are great, and needed, but what about the 2-5 second pause while you wait for the round trip to the server? It would be nice if client side code were easier to incorporate.

Derek Simon

Posted on 5/25/2004 @ 4:40 PM
I've always disliked the entire postback/view state model for the very same reasons stated above. URLs, remember, are the most important marketing aspect of the Web. Commercials, advertisements and bookmarks all depend on URLs, and sacrificing them for the sake of ease-of-development is simply downright ignorant. I won't even mention SEO or site indexing. Browsers simply can't handle these rich content changes (postbacks). Think about it. Can you address (using a URL) a certain context in a Windows application? No, of course not. That kind of structure simply doesn't work. The same applies to Web Forms.

Kiran Makkapati

Posted on 5/25/2004 @ 5:54 PM
It is unfortunate that ASP.Net didn't provide a framework for remote scripting. Post back gets really ugly, if your application needs to globalize, normalize user input and apply business rules, give an option for user to correct the input when a user tabs of from one control to other control. If the form has really lage number of fields and many of them have business rules posting back so many times gets really annoying.

Raj Kaimal

Posted on 5/25/2004 @ 9:34 PM
I think that the idea of the webserver sending down an app while keeping the URL the same would be excellent in the case of a data entry WebForm. You have the user performing multiple edits/updates, where the URL really does not matter, through asynchronous callbacks. Users, in this case, will not be able to click the back button to see a “dirty” state of the Webform. To prevent this, we are currently using a combination of JavaScript, session state and browser cache expiry depending on the situation.

Another thing to consider is ADA compliance (http://www.w3.org/WAI/). If you have a page that morphs content without posting back, would it be ADA compliant or does it really matter? Example: How would a blind student submitting an online application to a University be able to use this?

There are cases where the URL matters too. One example would be email notifications where the URL contains information about what action to perform after authentication. As others have mentioned, bookmarking and indexing are also very important.

Finally, what will be the impact on security as we start to expose more and more client side code?

Sergio Pereira

Posted on 5/26/2004 @ 6:17 AM
Here goes a suggestion that has helped me in some applications. I created this server control that shows up right in the middle of the page (middle of the visible part) everytime the form is submitted. It is kind of a Wait or Request in Progress modal window (it's not really a window, but a DIV. But you get the idea).
This does not solve the problem with the postbacks, but it helps the users knowing that something is happening and they should wait, not clicking anything else on the form.
I have been a fan of XMLHTTP in the past but the kind of client script DHTML code that one has to write is so complicated and error-prone (especially comparing javascript editors/debuggers with .net languages) that the development time was always jacked up many times over.

Jonathan

Posted on 5/26/2004 @ 7:15 AM
I think the thing that has bothered me so much about ASP.NET is that it attempts to hide so much from the developer and make web application development like desktop application development with Visual Basic, when the two are very far apart! So much effort seems to have been spent on ViewState and PostBack, when these are things that web application developers don't really do very often. It seems that this trend will continue in Whidbey. ASP.NET appears to be targeted mainly at the average VB application designer who knows little to nothing about HTML and how Web Applications work. What about those of us who know how they work?

I don't want a "better" ViewState, or a more "powerful" PostBack... I don't ever use either. I don't want a "Form Designer" that I can't use to write valid XHTML and that mangles my markup every time I switch to it. I don't want controls that spit out text using printlines like the bad old days of Java Servlets. I want powerful templating systems with elegant automatic state management and an easy way to manage the flow of data *between* pages. I want bookmarks to work. I want the back button to work. How about being able to re-use WebUserControl's between applications like other Controls? How about powerful XHTML/CSS layout support? I really would like it if Microsoft would stop trying to create solutions to problems I don't have, and start solving the problems I do have as an experienced web application developer who cares about usability, standards, and cross-browser support.

All that being said, I have been very impressed with the overall platform of .NET, and I have been able to overcome a few of these problems on my own, although not yet in a general case. Microsoft has been very receptive to the feedback from developers, and I hope that they keep listening so that ASP.NET can become more applicable to the needs of experienced web developers.

Nikhil Kothari

Posted on 5/26/2004 @ 12:22 PM
Great comments! I agree with the 3 issues raised: back button, refresh and bookmarking. You'd definitely want to have the ability for an application to proffer up a set of entry points (in the form of URLs + optional context), which can be navigated to directly, and as a result be added to favorites, navigated back to etc. Refresh somewhat falls out of this as well - its like re-navigating to the same page. The page could have context information stored either on the server (eg. data that has been updated via the UI), or session-specific context stored in user state offered by the browser, when required. I do think there are a class of scenarios where a refresh taking you back to the initial state of the page is the normal thing to do (eg. Search results: Refreshing takes you back to the first set of records). Of course some of this is wishful thinking - you truly need the Web browser to understand an "application". Today all it understands is a URL to a single page, and misses out on the relationship amongst them.

One comment below talks of the missing functionality of remote scripting in ASP.NET v1. I'll post shortly on callbacks as well, which should help enabling some new scenarios without requiring everyone to build the underlying framework.

On the subject of better support for things like XHTML, I plan on posting specifically what we are doing in the Whidbey timeframe as well. Stay tuned.

David

Posted on 5/28/2004 @ 11:48 AM
OT: Most people would probalby agree with me when I say that yours and Vandana Datyes book "Developing Microsoft ASP.NET Server Controls and Components" is the best out the on the topic. So my question is: Will there be sequel that covers building controls using the Whidbey model?

Luciano Evaristo Guerche

Posted on 5/29/2004 @ 7:25 AM
Hope to hear more about Whidbey's Callbacks on your blog

Stephen Cassels

Posted on 6/10/2004 @ 3:06 AM
I also agree with your points. I'm not so sure about your <tangent> viewpooint, usability wise it is essential that the user can bookmark.

On the issue of maintaining page state, like control focus, scroll position I used the register client script method. Error reporting complicates matters; for example we use the client side validators summary view dialog instead of sticking messages in the html like a span. The reason being that if the user has scrolled down the page they could miss the error message. In an effort to maintain UI consistency, that is, use dialogs for error messages from the server side, when a business rule on the server side is broken we use the register client script to display an error dialog with the server message. Simply sticking the error message in a span means that the user could miss it and creates problems for the current scroll position. Is this sort of thing handled in Whidbey?

One of the reasons I ended browsing to your blog was that I am trying to implement a large treeview (350 - 400k) in a internet solution. Obviously dynamic loading is a must and raises the issues of finding an alternative to postback and viewstate, namely XMLHTTP. The latter is a technology that I'm only beginning to dabble with.

I was hoping to find an article on the implementation the MSDN Library TreeView, I'm sure there used to be one but I can't find it now, can you help or do you have more on the implementation of the call back method? It sounds like a XMLHTTP or a dom implementation.

Corbin Hoenes

Posted on 6/21/2004 @ 11:28 AM
I am very interested in taking a look at the design of this clientside framework that these guys built. It sounds a lot like using what I think Callbacks will become. I too have thought about trying to create some kind of a framework to allow partial page updates (inside divs or updating select elements / text boxes via XMLHTTP).

Has anyone seen any complete framework that does a good job of this? And is there a ASP.NET binding for it?

Ashish B

Posted on 6/25/2004 @ 1:16 PM
I was just wondering if the new ASP 2.0 Client Callback feature is going to work on browsers other than I.E. or is it totally an IE specific feature. We have a reuqirement for a new app to have some pages which can refresh without a postback, but we need to support netscape as well. Thanks!

Daniel Cazzulino

Posted on 7/4/2004 @ 9:15 AM
As long as the underlying infrastructure (XMLHTTP and callbacks) continue to use HTTP GETs for subsequent resources used to morph the UI, the underlying architecture of the WWW will continue to serve its purpose by proper scaling. Bookmarking is hurt certainly, and that's not good IMO. And you generally lose "addressability" of your application resources/content too. Not good either.
How are you supposed to send a link to a representation of a purchase order if it's only a GET returning an XML you're using to morph a client UI?

mayur pradhan

Posted on 7/13/2004 @ 8:09 AM
is it possible in the current asp.net to maintain scroll position , without using javascript???

josh

Posted on 7/17/2004 @ 6:32 AM
What about smartnavigation! I makes postbacks a lot more tolerable.

Onkelborg

Posted on 7/25/2004 @ 3:03 PM
I've got a wonderful idea...

Why not use the http standard? If you return the code 204 the browser will ignore whats coming back! If you really want the result, why not use the iframe? It's also a standard...

Abhisek Banerjee

Posted on 8/9/2004 @ 9:08 PM
This is regarding the tangent Q that you had posted. The idea of having navigation around the app without having the URL change in the address bar is fine for some web apps. Others would prefer the URL to change as the user navigates to allow the user to bookmark the articular page that he/she is on. In case you are working on a framework that may allow such a navigation or post back, I would request you to leave it to the developers using the framework whether they want the URL to change or not.

Cory

Posted on 12/6/2004 @ 3:11 PM
I am aware that this is a slippery slope, but the entire notion of using IE, or any other well matured DHTML supporting browser, as an application container, rather than a HTML viewer is, and will continue to be, both a huge positive and negative as a supportable environment.

The key issue, in my mind, is the lack of contextual shift on the part of the user. From a developer or architect perspective, it is readily apparent that when hosting the functionality of an application via dhtml and asynch callbacks in a browser, the inherent functionality provided via history/back/forward, etc, are no longer of any applicable use. However, our minds already understand the functionality, and we know WHY the back button won't work, and so on. The user does not understand this, and won't, period.

That said, and also understanding that it is not always an option to distribute the .NET runtime to desktops, continually manage distributed patches, etc: The control over functionality and context for the back and forward buttons needs to be able to be controlled by the hosted application. Please note, that when I say "hosted" I don't mean active document - I mean pure ASP.NET/dhtml.

1) You gain all of the benefit provided by using IE as the application host.
2) You would be able to avoid the inevitable call of "when I hit the back button....."

This could be possible, but only with obvious attention paid to the inherent security risk this opens up. To begin with, it could be possible to only allow this to occur when the site is both Trusted and digitally signed, etc.

The reality, imho, is that when internal support is provided for callbacks in Whidbey, more and more will we see this as the implementation method of choice, rather than post-backs, but will still have to deal with the fact the users still want to use their back buttons, whether on the browser bar, or built into their mouse.

Developer contextual control over the back/forward navigation, via trappable events, would allow for client state and context management, keeping the users happy, and calls away from corp. help desks, in addition to keeping the processing on the client, rather than round-tripping repeatably.

Cory

Amir Leshem

Posted on 12/21/2005 @ 3:21 PM
Why not turning Postbacks to AJAX-Postbacks?
That's exactly what zumiPage automatically does.
http://www.zumipage.com
The discussion on this post has been closed. Please use my contact form to provide comments.