views:

15

answers:

2

I cannot get Javascript to run after a ASP.NET postback from a client script block injected from a master page

Below is my master page logic, and it gets hit on a postback, I have confirmed with a breakpoint.

namespace MyAwesomeProjectThatWillTakeOverTheWorldIfNotForThisIssue
{
    public partial class CommonContent : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), 
            "script", 
            "alert('Success!');", 
            true);
        }
    }
}

The problem is the alert popup only appears on the first load but not on postback.

What am I not doing right?

+1  A: 

If this is an ajax triggered postback you need to use Scriptmanager.registerstartupscript

Achilles
Try referencing the current child page and pass that into the scriptmanager.
Achilles
+2  A: 

If you are using ASP.NET AJAX, and have a script manager on the page, consider using ScriptManager.RegisterStartupScript().

kbrimington
+1 I think I referenced the wrong function initially.
Achilles
@Achilles, honestly, I'd be dead in the water without Intellisense, what with the subtle differences between method names and scope (static vs. instance) between `ScriptManager` and `Page.ClientScript`.
kbrimington