Click here to Skip to main content
15,881,938 members
Articles / Web Development / HTML

Load and Display Page Contents Asynchronously with Full Postback Support

Rate me:
Please Sign up or sign in to vote.
4.87/5 (72 votes)
14 Sep 2010Ms-PL9 min read 585.9K   4.3K   242  
An AJAX UpdatePanel with less communication overhead and better performance
// Copyright (c) iucon GmbH. All rights reserved.
// For more information about our work, visit http://www.iucon.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Web.Script.Serialization;

namespace iucon.web.Controls
{
    class PartialUpdatePanelHandler : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Form["__USERCONTROLPATH"] != null)
            {
                try
                {
                    bool isControlPathEncrypted = context.Request.Form["__ENCRYPTED"] != null;
                    string controlPath = isControlPathEncrypted ? Encryptor.DecryptFromString(context.Request.Form["__USERCONTROLPATH"]) : context.Request.Form["__USERCONTROLPATH"];
                    string controlClientID = context.Request.Form["__CONTROLCLIENTID"];
                    IDictionary scriptInfo = DeserializeScriptManagerInfo(context);

                    PanelHostPage page = new PanelHostPage(controlPath, controlClientID, scriptInfo);

                    ((IHttpHandler)page).ProcessRequest(context);

                    context.Response.Clear();
                    context.Response.Write(page.GetHtmlContent());                    

                }
                catch (Exception ex)
                {
                    // Prevent ScriptModule from reformatting the exception
                    if (HttpContext.Current.Items["System.Web.UI.PageRequestManager:AsyncPostBackError"] != null)
                        HttpContext.Current.Items["System.Web.UI.PageRequestManager:AsyncPostBackError"] = false;
                    
                    if (ex.InnerException != null)
                    {
                        context.Response.Write(ex.InnerException.Message.Replace("\n","<br />"));
                        context.Response.Write("<hr />");
                        context.Response.Write(ex.InnerException.StackTrace.Replace("\n", "<br />"));
                    }
                    else
                    {
                        context.Response.Write(ex.Message.Replace("\n", "<br />"));
                        context.Response.Write("<hr />");
                        context.Response.Write(ex.StackTrace.Replace("\n", "<br />"));
                    }                    
                }
            }
        }

        private static IDictionary DeserializeScriptManagerInfo(HttpContext context)
        {
            string scriptManagerInfo = context.Request.Form["__SCRIPTMANAGERINFO"];
            Hashtable info = new Hashtable();

            if (!string.IsNullOrEmpty(scriptManagerInfo))
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Parameter[] parameters = serializer.Deserialize<Parameter[]>(scriptManagerInfo);
                if (parameters != null && parameters.Length > 0)
                {
                    foreach (Parameter p in parameters)
                    {
                        info[p.Name] = p.Value;
                    }
                }
            }

            return info;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) iucon GmbH
Germany Germany
My name is Stefan and I am working at iucon GmbH (http://www.iucon.de), a german software development company.
Our main focus is the development of online shops including the ERP backend.
We also develop custom software solutions for our customers and are able to support other companies and developers in using the following technologies, because we use them every day Wink | ;-)

- .NET Framework 2.0
- .NET Framework 3.0
- .NET Framework 3.5
- Windows Workflow Foundation
- C#
- ASP.NET
- ASP.NET AJAX
- SQL Server 2005 T-SQL
- ADO .NET
- Reporting Services
- Integration Services
- XML Webservices
- Database Design
- Payment systems

You can find information about our ERP software iuBIZ on http://www.iubiz.de which is totally written in .NET by using most of the cool new technologies like Workflow Foundation, Reporting Services, ClickOnce-Deployment. Feel free to take a look at the website and see Microsoft's new technologies in action Wink | ;-)

Comments and Discussions