无法使用更新面板从代码隐藏触发 JavaScript 方法 [英] Unable to trigger JavaScript method from code-behind using Update Panel

查看:19
本文介绍了无法使用更新面板从代码隐藏触发 JavaScript 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题详情:

在数据库中保存数据后,我需要向用户显示警报.为此,我从代码隐藏注册了脚本.但是它不适用于更新面板,而在没有更新面板的情况下它可以按预期工作.我需要让它与更新面板一起工作.

After saving the data in DB, I need to show an alert to the user. For this, I have registered the script from code-behind. But it is NOT working with update-panel whereas it works as expected without update-panel. I need to make it work with update-panel.

我尝试在注册脚本时添加 $(document).ready(function () ,但是没有用.

I tried by adding $(document).ready(function () while registering the script, but it didn't work.

然后,我尝试使用 ScriptManager.RegisterClientScriptBlock() 方法代替 Page.ClientScript.RegisterStartupScript(),但它也不起作用.

Then, I tried with ScriptManager.RegisterClientScriptBlock() method instead of Page.ClientScript.RegisterStartupScript(), but it also didn't work.

这是网页(ASPX)的代码片段:

Here is the code snippet of web page (ASPX):

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Button ID="btnOutsideUpdatePanel" runat="server" Text="Outside UpdatePanel" OnClick="btnOutsideUpdatePanel_Click" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnInsideUpdatePanel" runat="server" Text="Inside UpdatePanel" OnClick="btnInsideUpdatePanel_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <uc1:Child runat="server" id="Child1" />
         <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <uc1:Child runat="server" id="Child2" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>

这是用户控件(ASCX)的代码片段:

Here is the code snippet of user-control (ASCX):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Child.ascx.cs" Inherits="UpdatePanelAndScript.Child" %>
<asp:Button ID="btnUserControl" runat="server" Text="User Control" OnClick="btnUserControl_Click" />

网页代码隐藏:

protected void btnOutsideUpdatePanel_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowSuccess", "alert('Hi');", true);
}

protected void btnInsideUpdatePanel_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowSuccess", "alert('Hi');", true);
}

用户控制的代码隐藏:

protected void btnUserControl_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowSuccess", "alert('Hi');", true);
}

推荐答案

你只需要修改如下语句:

You only need to change the following statement:

Page.ClientScript.RegisterStartupScript
(
    this.GetType(),
    "ShowSuccess",
    "alert('Hi');",
    true
);

致:

ScriptManager.RegisterStartupScript
(
    this,
    this.GetType(),
    "ShowSuccess",
    "alert('Hi');",
    true
);

参考:

注册部分页面更新兼容脚本
[...] 如果您要呈现在 UpdatePanel 控件中使用的脚本,请确保调用 ScriptManager 控件的方法.

Registering Partial-Page Update Compatible Scripts
[...] If you are rendering script for use inside an UpdatePanel control, make sure that you call the methods of the ScriptManager control.

这篇关于无法使用更新面板从代码隐藏触发 JavaScript 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆