二:
前台代码:
<div>
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Button ID="btnCommit" runat="server" Text="Button"
οnclick="btnCommit_Click" />
<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);