Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1048075
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 系统运维

2012-03-23 19:46:48

C#后台输出JS代码我们通常是

Response.Write("");

这样写非常不方便,因此去年10月份的时候特意写了一个封装类,共享在CSDN上,博文地址为http://blog.csdn.net/wenjie315130552/article/details/6934777

code:

1 ///
2 /// Alart 的摘要说明
3 ///

4 public class Alart
5 {
6 public enum AlartState
7 {
8 ///
9 /// 在根窗口中打开该连接
10 ///

11 OpenInTopWindow,
12 ///
13 /// 在父窗口中打开该连接
14 ///

15 OpenInParentWindow,
16 ///
17 /// 在当前窗口中打开该连接
18 ///

19 OpenInThisWindow,
20 ///
21 /// 返回到上一个页面
22 ///

23 Back,
24 ///
25 /// 关闭窗口
26 ///

27 CloseWindow,
28 ///
29 /// 不做任何操作
30 ///

31 Nothing
32 }
33
34 ///
35 /// 输出JS提示信息对话框
36 ///

37 /// 页面ContextHttpContext.Current
38 /// 消息内容
39 /// 输出模式*枚举
40 /// 提示后要转到的URL
41 public static void ShowAlart(HttpContext context, string msg, AlartState alartState,string toUrl)
42 {
43 StringBuilder mySB = new StringBuilder();
44 mySB.AppendFormat("");
66 context.Response.Write(mySB);
67 context.Response.End();
68 }
69
70 ///
71 /// 输出JS
72 ///

73 /// 页面ContextHttpContext.Current
74 /// 脚本
75 public static void UseJs(HttpContext context,string jsStr)
76 {
77 StringBuilder mySB = new StringBuilder();
78 mySB.AppendFormat("");
80 context.Response.End();
81 }
82 }

调用:

样式一(返回上一个页面):

Alart.ShowAlart(System.Web.HttpContext.Current, "密码与确认密码不一致", Alart.AlartState.Back, "");

样式二(在本窗口打开):

asp.net mvc:

Alart.ShowAlart(System.Web.HttpContext.Current, "操作成功", Alart.AlartState.OpenInThisWindow, Url.RouteUrl(new { controller = "ControllerName", action = "ActionName"}));

关于如何使用Url.RouteUrl(object routeValues)请参照wtq的博文使用Url.Routeurl获取url值。

asp.net:

Alart.ShowAlart(System.Web.HttpContext.Current, "操作成功", Alart.AlartState.OpenInThisWindow,"PageName"));

在asp.net中 尽量用 ClientScript.RegisterStartup() 或者 clientScript.RegisterBlock 类似的这样方法,因为Response.Write()直接输出在页面上,会导致页面变形。

阅读(440) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~