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

I used the method ScriptManager.RegisterStartupScript()to show a warning when a particular thing happens at the end. It works great in page load mode, but not in the specific method that is called up when you click on a specific button. I could not find a solution, because on another page it works fine both when loading the page, and in the method.

Script Manager Method RegisterStartupScript

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();
    }
}

The Wish method should be called at the click of a button.

protected void uploadDocSave_ServerClick()
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}
+4
source share
6 answers

Try the following:

, :

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);
0

, :

__doPostBack();

JavaScript.

0

, . RegisterStartupScript , , javascript. :

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

!

0

:

:

Use Literal Control
<asp:Literal ID="IMsg" runat="server" Text=""/>

:

string msg = "<script>alert('Change successfully');</script>";
IMsg.Text = msg;

, . .

0

<button runat="server"></button> onserverclick:

HTML:

<button class="some-class" id="btnSave" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

#:

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

JavaScript , onclick:

HTML:

<button class="some-class" id="btnSave" onclick="return btnSave_clientClick();" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

JavaScript:

function btnSave_clientClick() {

     // code

     if (some_condition) {

        return true; // allows the control to do a postback

     }
}

#:

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

My deepest condolences regarding the OP if you still need to work with Webforms. Thus, you can solve this problem in a minimal way while reducing traffic:

Code example:

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();
    }
}

Example web forms:

<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>

0
source

Source: https://habr.com/ru/post/1569686/


All Articles