In the previews post about Optimize your page for UpdatePanel I talked about removing white spaces from the ASPX to optimize the Update Panel callbacks because it can't be compressed as normal response, and thats due the special format that this response is (it needs to be parse be JS in the client side).
The Update Panel response contain the new html for the specific location that needs to be update and the complete new ViewState for the whole page. So, one more thing we can to optimize the UpdatePanel is to compress the ViewState before it been sent to the client. We don't need to do it in normal response because we use (or should use) a compression module that compress all the page response and that includes the ViewState. Compress the ViewState can save you some more KB from the async response. Another option is to save the ViewState in the server (file os session), and not send it at all. To compress the ViewState we needs to override the 'LoadPageStateFromPersistenceMedium' and the 'SavePageStateToPersistenceMedium' methods that load and save the view state.
Here is the code how to compress the ViewState for the UpdatePanel response only, just copy it to your base page:
protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__COMPRESSEDVS"];
if (viewState != null)
{
byte[] data = Convert.FromBase64String(viewState);
data = Utils.Decompress(data);
LosFormatter lf = new LosFormatter();
return lf.Deserialize(Convert.ToBase64String(data));
}
else
{
return base.LoadPageStateFromPersistenceMedium();
}
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
if (Utils.IsMsAjaxCallback(Request))
{
LosFormatter lf = new LosFormatter();
using (StringWriter writer = new StringWriter())
{
lf.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] data = Convert.FromBase64String(viewStateString);
data = Utils.Compress(data);
ScriptManager.RegisterHiddenField(this, "__COMPRESSEDVS", Convert.ToBase64String(data));
}
}
else
{
base.SavePageStateToPersistenceMedium(viewState);
}
}
(The class 'Utils' can be download here: Utils.cs (1.15 kb))