为了方便在c#中弹出网页alert对话框,我写了一个C#的Alert方法,调用时就像在写JS一样:
例如:Alert('提示信息');
此方法的代码如下:
#region 弹出警告消息
protected void Alert(string strMessage)
{
strMessage = strMessage.Replace('\n\r', '');
strMessage = strMessage.TrimEnd((char)'\n\r'.ToCharArray());
strMessage=strMessage.Replace(''', '\'').Replace(@'\', @'\\');
// Response.Write('<script>alert(''+strMessage+'')</script>')//这是不使用Ajax时的弹出方法
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('' + strMessage + '')" true); //使用Ajax后必须这样弹出
}
#endregion
有关Ajax中无法弹出alert对话框的原因(网上查的):
在使用asp.net ajax过程中可能会遇到这样的问题:就是想用js脚本输出一些提示,可是不管是用 Page.RegisterClientScriptBlock还是用 Page.RegisterStartupScript注册,该死的提示信息总是不出来。其实不是不出来,只是我们叫错了它们的名字,我们只要用下面的这条语句:
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), 'click', 'alert('提示信息')', true);
就可以正常的弹出alert对话框了。
注:如果在页面中有多个UpdatePanel,如果每个UpdatePanel的UpdateMode都是“always”的话,
那么你使用那个UpdatePanel的实例作为参数都可以;如果每个UpdaePanel的UpdateMode=conditional,
那么你必须使用正在更新的那个UpdatePanel作为参数,这样脚本才能起作用。
例如:Alert('提示信息');
此方法的代码如下:
#region 弹出警告消息
protected void Alert(string strMessage)
{
strMessage = strMessage.Replace('\n\r', '');
strMessage = strMessage.TrimEnd((char)'\n\r'.ToCharArray());
strMessage=strMessage.Replace(''', '\'').Replace(@'\', @'\\');
// Response.Write('<script>alert(''+strMessage+'')</script>')//这是不使用Ajax时的弹出方法
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('' + strMessage + '')" true); //使用Ajax后必须这样弹出
}
#endregion
有关Ajax中无法弹出alert对话框的原因(网上查的):
在使用asp.net ajax过程中可能会遇到这样的问题:就是想用js脚本输出一些提示,可是不管是用 Page.RegisterClientScriptBlock还是用 Page.RegisterStartupScript注册,该死的提示信息总是不出来。其实不是不出来,只是我们叫错了它们的名字,我们只要用下面的这条语句:
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), 'click', 'alert('提示信息')', true);
就可以正常的弹出alert对话框了。
注:如果在页面中有多个UpdatePanel,如果每个UpdatePanel的UpdateMode都是“always”的话,
那么你使用那个UpdatePanel的实例作为参数都可以;如果每个UpdaePanel的UpdateMode=conditional,
那么你必须使用正在更新的那个UpdatePanel作为参数,这样脚本才能起作用。
本文介绍了一种在C#中利用Ajax技术弹出JavaScript alert对话框的方法。通过一个简单的Alert方法实现类似JavaScript的弹窗功能,解决了在使用ASP.NET Ajax时常规方法无法正常显示alert的问题。

6119

被折叠的 条评论
为什么被折叠?



