ScriptManager.RegisterStartupScript() method not working - ASP.NET, C#

GMHSJ picture GMHSJ · Jan 2, 2015 · Viewed 34.8k times · Source

I have used ScriptManager.RegisterStartupScript() method in order to show an alert when particular thing happens in back end.It works fine in page load method but not in particular method which is called when a specific button is clicked . I Couldn't find a solution because in another page it works fine in both page load and the method.

Script Manager RegisterStartupScript Method

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);

HTML

<asp:HiddenField runat="server" ClientIDMode="Static" ID="PostBackController"/>
<button class="some-class" id="btnSave" runat="server" onclick="btnSave_clientClick();">SAVE</button>

Javascript

function btnSave_clientClick() {

     // code

     if (some_condition) {

        $('#PostBackController').val("btn_save");
        __doPostBack();

     }
}

Page Load Method

protected void Page_Load(object sender, EventArgs e)
{
    if (PostBackController.Value == "btn_save")
    {
        uploadDocSave_ServerClick();
    }
}

Method Wish should be called when button Clicked

protected void uploadDocSave_ServerClick()
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

Answer

Dbl picture Dbl · Oct 5, 2017

My deepest condolences for OP if you still have to work with Webforms. This is how you can solve it in a minimal way while reducing traffic:

Codebehind sample:

public partial class About : Page, IPostBackEventHandler
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // Unless the button is serverside clicking it will reload the page
        // registering the page like this prevents a page reload.
        var scriptManager = ScriptManager.GetCurrent(Page);
        scriptManager?.RegisterAsyncPostBackControl(Page);
    }

    /// <inheritdoc />
    public void RaisePostBackEvent(string eventArgument)
    {
        var javascriptCode = $"alert('server method called and triggered client again. {DateTime.Now.ToString("s")}');";

        // if your key isn't changed this script will only execute once
        ScriptManager.RegisterStartupScript(udpMain, typeof(UpdatePanel), Guid.NewGuid().ToString(), javascriptCode, true);

        // updating the updatepanel will inject your script without reloading anything else
        udpMain.Update();
    }
}

Webforms sample:

<script type="text/javascript">
    function clientFunction(sender, args) {

        alert('client function called');

        <%= Page.ClientScript.GetPostBackEventReference(Page, "callServerFunction") %>

        args.preventDefault();
    }
</script>

<asp:UpdatePanel runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional" ID="udpMain">
    <ContentTemplate>
        <h2><%: Title %>.</h2>
        <h3>Your application description page.</h3>
        <p>Use this area to provide additional information.</p>
        <button onclick="clientFunction(); return false;">raise server function</button>
    </ContentTemplate>
</asp:UpdatePanel>