Friday, 12 February 2010

Trick for creating a ‘notification’ screen without AJAX – ASP.Net 1.1

If you are developing on ASP.Net 1.1, the first thing which makes you lose the will to live (especially, if you’re accustomed to ASP.Net Ajax) is obviously the missing AJAX framework!

There is very little you could do with ASP.Net Forms for example, displaying a notification screen when you are processing something in the background after a post pack.

Without using jQuery , Javascript XMLHttp Requests or an open source AJAX framework for ASP.Net 1.1 your probably stumped , so was i., until i found out a trick to do something similar but with a post back (oh, what the heck!, but it still works, given the fact that i can display a notification screen before i start a lengthy process on the server.)

The Trick.

0] Create a session object , let it be an instance of a class which has some properties than can be set.

1] In the Form_Load hander, invoke your lengthy process depending on a Boolean property in your session object.

3] On the dreadful postback, when you want to start the ‘lengthy process’, Set your session object’s Boolean property and do a Response.Redirect(“yourwaitingscreen.aspx”). This will show the waiting screen immediately, however you have still not started the lengthy process.

4] Finally, starting the lengthy process is easy.

Add this in your <meta> tag to “yourwaitingscreen.aspx” to do the magic.

<meta http-equiv=”refresh”; content=1 url=”your_lengthy_process_aspx_page.aspx”>

I can hear the Gotchas !!! and ahas!!! now.

The way asp.net processes the request is still synchronous., remember no AJAX either. But, we are taking advantage of the synchronous processing itself. i.e when we add the

‘<meta http-equiv=”refresh”; content=1 url=”your_lengthy_process_aspx_page.aspx”>’ line to

“yourwaitingscreen.aspx”, you are loading the page, which starts the ‘lengthy process’, but since you are already in the ‘waiting screen’, you need not do anything. you also get a progress bar with IE and Firefox etc. (bonus!!!)

Drawbacks:- postbacks and logic in your Form_Load to start the ‘lengthy process’ (can be accomplished using a session variable as discussed above)

But, that’s fairly trivial given the fact that you are telling the user that you are doing something in the background which is going to take some time. (as simple as that.) – with a progress bar inherently displayed by the browser.

Advantages

It’s maintainable because the ‘lengthy process’ can be loosely coupled with your UI or service. You can re-factor/enhance/test your code for ‘lengthy process’ in isolation, which makes it scalable and may be reusable!

No comments:

Post a Comment