ASP.NET - Register Startup Script from a postback inside an update panel.

Posted on 2011-10-20
Last Modified: 2012-06-22
I have a situation where I have a button inside of an update panel.  When the button is clicked it triggers a partial page postback.  In the code behind of that partial page postback I am trying to register a client startup script based on logic that is running in the server side click event.  The code I'm trying to use to do this is...

ClientScript.RegisterStartupScript(this.GetType(), "Displayt36SaveDetailsCompleteMessage", "Displayt36SaveDetailsCompleteMessage();", true);

But when the partial page postback completes, this code is not being run.  Is there a way I can register a script in the code behind of a partial page postback of an update panel and have that script run as soon as the update panel finishes the postback?

Question by:JosephEricDavis
    LVL 20

    Accepted Solution

    Ok well when I first read your question I didn't think it was possible, or at least not easily possible.

    On the other hand it seemed like a reasonable thing to want to do so I got curious, then I started testing and the answer turns out to be Yes!

    So I have a simple test page setup that should answer your question<see code>
    ****ASPX PAGE****
        <form id="form1" runat="server">
    	<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" >
    		<asp:Label ID="LbStatic" runat="server" />
    		<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    			<asp:Button ID="BtnTrigger" runat="server" Text="Fire" 
    				onclick="BtnTrigger_Click" /><br />
    			<asp:Label ID="LbUpdate" runat="server" />
    ****CODE BEHIND****
        protected void Page_Load(object sender, EventArgs e)
            LbStatic.Text = DateTime.Now.TimeOfDay.ToString();
        protected void BtnTrigger_Click(object sender, EventArgs e)
            LbUpdate.Text = "AJAX Time: " + DateTime.Now.TimeOfDay.ToString();
            string script = "alert('fired');";
            //Note this is not the id of the scriptmanager as an object on the front end
            //Its just ScriptManager
            //Note this only fires when the trigger performs a partial page postback

    Open in new window

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Ok try this instead:
    ScriptManager.RegisterStartupScript(this.YourUpdatePanelId, typeof(string), "Displayt36SaveDetailsCompleteMessage", "Displayt36SaveDetailsCompleteMessage();", true);

    Open in new window

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Oops sorry, the correct reference for that method is:

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now