ASP.NET中像使用MessageBox一样执行JS的消息框脚本

翻看多年前的例子,发现了这样的一个小类,能够让ASP.NET服务器端代码像WinForm的MessageBox.Show一样快速的弹出消息框。具体的实现还是用JS实现的,而且并非我原创,只是年代久远,已经记不起从哪里找到的了。现在的解决方案很多,Ajax的效果都有了,这个就写下来留念一下吧。主要原理还是使用ClientScriptManager的RegisterStartupScript方法实现。不罗嗦,上代码。

查看代码
 1 /// <summary>
 2 ///  在Web中模仿MessageBox的消息框
 3 /// </summary>
 4 public static class ShowMessage
 5 {
 6     /// <summary>
 7     /// 显示警告框的客户端脚本
 8     /// </summary>
 9     /// <param name="objPage">需要显示警告框的页面,一般用this调用</param>
10     /// <param name="message">提示的内容</param>
11     public static void Alert(Page objPage, string message)
12     {
13         string key = "Alert";
14         string script = string.Format("alert('{0}');", message);
15         objPage.ClientScript.RegisterStartupScript(objPage.GetType(), key, script, true);
16     }
17 
18     /// <summary>
19     /// 显示警告框后,跳转地址
20     /// </summary>
21     /// <param name="objPage">需要显示警告框的页面,一般用this调用</param>
22     /// <param name="message">提示的内容</param>
23     /// <param name="url">提示后跳转的URL</param>
24     public static void Alert(Page objPage, string message, string url)
25     {
26         string key = "Alert";
27         string script = String.Format("alert('{0}');window.location='{1}';", message, url);
28         objPage.ClientScript.RegisterStartupScript(objPage.GetType(), key, script, true);
29     }
30 }

服务器端调用的代码

ShowMessage.Alert(this, "提示信息");

ShowMessage.Alert(this, "提示信息""跳转页面.aspx");

转载于:https://www.cnblogs.com/Bonizlee/archive/2012/04/16/2451656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值