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

标签 javascript c# jquery asp.net scriptmanager

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

脚本管理器 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();

     }
}

页面加载方法

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

点击按钮时应调用方法 Wish

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

最佳答案

如果您仍然需要使用 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();
    }
}

网络表单示例:

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

关于javascript - ScriptManager.RegisterStartupScript() 方法不起作用 - ASP.NET、C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740632/

相关文章:

javascript - jQuery:如何 .show() 具有包含特定文本的类的元素?

javascript - 如何不映射缺少子值的对象

javascript - 转到Vuejs 2中的特定路线

c# - 为代码覆盖关闭 Visual Studio 2010 中的 Lambda 表达式

c# - 如何从数据库字段中获取 <li> 的值?

javascript - eBay 使用 jQuery 自动完成/建议

javascript - 检查是否为 401、404、500、其他 jquery/javascript

javascript - 如何使用 for 循环来控制台记录数组的每个项目?

c# - WixSharp 注入(inject)多个自定义 CLR 对话框

jQuery for 循环切换 id