Triggering an ASP.NET AJAX UpdatePanel refresh with JavaScript

Thread is closed for posting
1 posts, 0 answers
  1. A9E74E7C-52FA-4440-8D67-C26A0770B01B
    A9E74E7C-52FA-4440-8D67-C26A0770B01B avatar
    16 posts
    Member since:
    May 2004

    Posted 08 Feb 2007 Link to this post

    Requirements

    r.a.d.controls version Q4 2006
    .NET version ASP.NET 2.0 + ASP.NET AJAX
    Visual Studio version

    2005
    programming language

    C# + JavaScript
    browser support

    all browsers supported by r.a.d.controls


     
    PROJECT DESCRIPTION
    Sometimes we need to trigger an UpdatePanel update from custom JavaScript, most often in a DOM event handler. For example we may want to refresh a RadGrid control, so that new data gets displayed to the user.

    We can use a regular __doPostBack() function call to that while passing the grid unique ID. ASP.NET AJAX's PageRequestManager object will detect that and turn it into an Ajax request coming from the grid inside the UpdatePanel. This will update the panel if we have the ChildrenAsTriggers property set to true (this is the default).

            <script type="text/javascript"
            function pageLoad() 
            { 
                $addHandler($get("postBackButton"), "click", Function.createDelegate(null, GridPostBack)); 
            } 
         
            function GridPostBack() 
            { 
                __doPostBack('<%=RadGrid1.UniqueID %>'"customPostback"); 
            } 
                 
            </script> 

    We will have to override the RaisePostBackEvent page method on the server to intercept our custom postback and do something of value:

        protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument) 
        { 
            //is this our own custom postback? if not, let the base implementation handle it 
            if (eventArgument == "customPostback"
            { 
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "customPostBack""Sys.Debug.trace('customPostBack')"true); 
            } 
            else 
            { 
                base.RaisePostBackEvent(sourceControl, eventArgument); 
            } 
        } 
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.