ScriptManager.RegisterStartupScript() 方法不起作用 - ASP.NET、C#

GMH*_*HSJ 4 javascript c# asp.net jquery scriptmanager

我使用了ScriptManager.RegisterStartupScript()方法,以便在后端发生特定事件时显示警报。它在页面加载方法中工作正常,但在单击特定按钮时调用的特定方法中不起作用。我找不到解决方案,因为在另一个页面中,它在页面加载和方法中都工作正常。

脚本管理器RegisterStartupScript方法

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

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

JavaScript

function btnSave_clientClick() {

     // code

     if (some_condition) {

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

     }
}
Run Code Online (Sandbox Code Playgroud)

页面加载方法

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

单击按钮时应调用 Wish 方法

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

Dbl*_*Dbl 5

如果您仍然需要使用 Webforms,我对 OP 表示最深切的哀悼。您可以通过以下方式以最少的方式解决该问题,同时减少流量:

代码隐藏示例:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

网络表格样本:

<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>
Run Code Online (Sandbox Code Playgroud)