The World’s Leading Microsoft .NET Magazine
   
 
timstall

Donate Today!

Search Box

 

Calendar

««Sep 2010»»
SMTWTFS
    1234
567891011
12131415161718
19202122232425
2627282930

My RSS Feeds








Mailing List

Most Popular Tags

                                                           

Avoiding unnecessary server work in ASP.Net

posted Tuesday, 12 May 2009

One of the best ways to increase performance in ASP.Net pages is to not do any unnecessary server-side work. For example, every postback and callback will re-trigger the entire Page's server life cycle - rerunning OnInit, OnLoad, OnPreRender, etc... However, a server-side action only require a fraction of the original code to run - for example clicking a button may not require you to repopulate all dropdowns.

ConceptExampleDetails
Is PostbackClicking an ASP.Net button postbacks the pagethis.IsPostBack
Is CallbackA JavaScript triggers an ASP.Net 2.0 callbackthis.IsCallback
Is RedirectingA method has called Response.Redirect, but there is still heavy processing (that is now unnecessary) that will still be called.Response.IsRequestBeingRedirected
Is Ajax update panel postbackA button within an Ajax update panel has been clicked, but don't redo server work outside of that update panelpublic static bool IsPartialPostback(System.Web.UI.Page p)
{
return ((System.Web.UI.ScriptManager.GetCurrent(p) != null)
&& (System.Web.UI.ScriptManager.GetCurrent(p).IsInAsyncPostBack))
}
Is Button clickUser clicks exit button - no need to repopulate dropdownsRequest["__EVENTTARGET"] //indicates which event (like a button) triggered the postback.

 

tags:  

links: digg this    technorati    




1. Jeff left...
Tuesday, 12 May 2009 8:17 am

You must have been reading my mind, I was researching some of these methods this morning, especially the IsCallBack and the IsRequestBeingRedirected the MSDN documentation is not really helpful on these 2 methods, I was wondering if you might provide a couple samples of when they would be useful?

IsCallback I understand is a JavaScript callback like Ajax or something. But the redirecting? How the heck does this actually get wired in when exactly does this event fire, it this on the page.init or page.load or when really and how do you properly use it?


2. Tim Stall left...
Tuesday, 12 May 2009 9:16 am

Hey Jeff, Good question. I don't have a short, easy-to-reproduce example. But what I have seen is that in larger frameworks, sometimes the page will redirect, but then it will still continue doing processing in other methods further down in the page life cycle (I think I've seen this in the Page.OnPreRender method). So, I'd use this step for tuning - if you find your framework still runs a method even after the page has somehow redirected, consider adding a check to Response.IsRequestBeingRedirected.