Remove View State from UpdatePanel Call

You may not be aware that view state (and control state) is included on each UpdatePanel call, even if the controls placed inside it have it disabled. As view state can get quite large, it may be useful to disable posting it during UpdatePanel calls, as long as you know what you are doing!

Here’s one possible solution, using Microsoft’s AJAX Library to intercept the asynchronous call before it is actually sent to the server:

   1: function removeViewState(body)
   2: {
   3:     var builder = new Sys.StringBuilder();
   4:     var parts = body.split('&');
   5:  
   6:     for (var i = 0; i < parts.length; ++i)
   7:     {
   8:         if (parts[i].indexOf('__VIEWSTATE=') < 0)
   9:         {
  10:             builder.append(parts[i]);
  11:         }
  12:  
  13:         if (i != parts.length - 1)
  14:         {
  15:             builder.append('&');
  16:         }
  17:     }
  18:  
  19:     return (builder.toString());
  20: }
  21:  
  22: Sys.Net.WebRequestManager.add_invokingRequest
  23: (
  24:     function (s, e)
  25:     {
  26:         var webRequest = e.get_webRequest();
  27:         var body = webRequest.get_body();
  28:  
  29:         body = removeViewState(body);
  30:  
  31:         webRequest.set_body(body);
  32:     }
  33: );

As you can see, I iterate through all of the parameters being sent along with the request, and I skip the view state.

Hope this helps!

                             

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website