.net控件ScriptManager和UpdatePanel无法弹出窗口的问题

来源:互联网 发布:淘宝一个皇冠要多少丹 编辑:程序博客网 时间:2024/04/29 20:32

.net使用ScriptManager和UpdatePanel无法弹出窗口如何解决

.net使用静态刷新控件ScriptManager和UpdatePanel无法弹出窗口如何解决

如果在页面里面用到了ScriptManager,里面再放个更新面板,那客户端的弹出对话框就不起作用了。

例如:

Page.ClientScript..RegisterStartupScript(this.GetType(), "", "<script>alert('添加信息成功!');</script>"); 

这句验证就失去了作用。


那就要到以下的:


一:

ScriptManager.RegisterStartupScript(this.btnOK, this.GetType(), "alert", "alert('添加信息成功!');", true);

第一个参数: 是哪个按钮要使用的这个JS 

最后一个参数:使用了true ,所以前面的js里面就不能添加</script> 。添加就会出错的。

asp.net弹出信息框——用ScriptManager

二:

前台代码:

<div>
        <asp:ScriptManager ID="sm" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="up" runat="server">
        <ContentTemplate>
            <asp:Button ID="btnCommit" runat="server" Text="Button"
                onclick="btnCommit_Click" />
            &nbsp;
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>

后台代码:

protected void btnCommit_Click(object sender, EventArgs e)
    {
        ///获取弹出对话框的按钮
        Button button = (Button)sender;
        ///注册对话框的脚本
        ScriptManager.RegisterClientScriptBlock(button, button.GetType(), button.UniqueID, "alert('这是AJAX Web环境中的对话框。');", true);

}


项目例子如下:

 protected void btnSelect_Click(object sender, EventArgs e)
        {
            //Response.Redirect("SelectCustomer.aspx");
            //特别提示(经验):UpdatePancel控件中不能使用response.write输出否则报错,有冲突.可以使用response.redirect("ynkmit.html?id=1")进行页面跳转,  故以下2个不可用
            //HttpContext.Current.Response.Write("<script language=javascript>var aa=window.open("SelectCustomer.aspx");aa.focus()</script>”) ;
            //HttpContext.Current.Response.Write("<script language=javascript>window.open('SelectCustomer.aspx','','width=200,height=200')</script>");

            //如果在页面里面用到了ScriptManager ,里面再放个更新面板,那客户端的弹出对话框就不起作用了  ,故以下2个不可用
            //ClientScript.RegisterClientScriptBlock(GetType(), string.Empty, "<script language=javascript>window.open("SelectCustomer.aspx")</script>");
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "show", "alert('设置首页摘要成功');", true);

            Button button = (Button)sender;   //获取弹出对话框的按钮
            //注册对话框的脚本
            System.Web.UI.ScriptManager.RegisterStartupScript(button, button.GetType(), button.UniqueID, "window.open('SelectCustomer.aspx')", true);

 

        }


PS:

因为ScriptManager是AJAX控件,页面使用AJAX进行无刷新改动,改完后发现原来程序中Response.Write("")都不能使用,有冲突,解决方法:

可以使用 System.Web.UI.ScriptManager.RegisterStartupScript(Contrl control, Type type,string key,string script, bool addScriptTags)来达到相应的目的。

其中control是引起回发事件的控件ID,type客户端脚本类型,key脚本块标识,script要执行的脚本程序,addScriptTags 如果为true 则前面的脚本语句中不需要加标识若为false 则要写出完整的脚本语句实例:

 ScriptManager.RegisterStartupScript(btnadd, this.GetType(), "ShowMessage", "alert('成功');",true)

或者另外种方法可以用HttpContext.Current.Handler来代替controlID,不过使用前需要转化为System.Web.UI.Page.

实例:

ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.Handler, this.GetType(), "ShowMessage", "alert('成功');", true);





原创粉丝点击