• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

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

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?

Thanks
0
JosephEricDavis
Asked:
JosephEricDavis
  • 2
1 Solution
 
ddayx10Commented:
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:ScriptManager>
    <div>
		<asp:Label ID="LbStatic" runat="server" />
		<asp:UpdatePanel ID="UpdatePanel1" runat="server">
		<ContentTemplate>
			<asp:Button ID="BtnTrigger" runat="server" Text="Fire" 
				onclick="BtnTrigger_Click" /><br />
			<asp:Label ID="LbUpdate" runat="server" />
		</ContentTemplate>
		<Triggers></Triggers>	
		</asp:UpdatePanel>
    </div>
    </form>



****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
        ScriptManager.RegisterClientScriptBlock(
            BtnTrigger,
            typeof(Button),
            "PartialOnlyScript",
            script,
            true);
    }

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Ok try this instead:
ScriptManager.RegisterStartupScript(this.YourUpdatePanelId, typeof(string), "Displayt36SaveDetailsCompleteMessage", "Displayt36SaveDetailsCompleteMessage();", true);

Open in new window


Reference:
http://msdn.microsoft.com/en-us/library/bb310408.aspx
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Oops sorry, the correct reference for that method is:
http://msdn.microsoft.com/en-us/library/bb359558.aspx
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now