项目中常用的几个JS
2008-03-27 15:29 蓝之风 阅读(418) 评论(1) 编辑 收藏 举报
这里总结一下项目中常用到的几个非常实用的Js脚本,为了方便我将其封装成一个C#的Js工具类方便在项目中使用,可以在项目中直接使用这些方法就可以了。这里是没有任何页面逻辑的方法可以单独拿出来使用,这个类可以继续丰富下去,会在以后加入一些新的方法
1/// <summary>
2 /// 通用的JS脚本
3 /// </summary>
4 public class JSUtils
5 {
6 public JSUtils()
7 {
8 //
9 // TODO: 在此处添加构造函数逻辑
10 //
11 }
12
13 public static Page CurrPage
14 {
15 get
16 {
17 return HttpContext.Current.Handler as Page;
18 }
19 }
20 /// <summary>
21 /// 向一个控件添加一个确认对话框.(如果取消则不提交服务器端)
22 /// </summary>
23 /// <param name="control">需要添加确认的控件</param>
24 /// <param name="message">对话框中显示的文本</param>
25 public static void AddConfirmToControl(IAttributeAccessor control, string message)
26 {
27 message = message.Replace("\\", "\\\\").Replace("\n", "\\n").Replace("\'", "\\'");
28 control.SetAttribute("onclick", "return confirm('" + message + "');");
29 }
30
31 /// <summary>
32 /// 直接关闭页面(忽略其他提示)
33 /// </summary>
34 public static void ClosePage()
35 {
36 HttpContext.Current.Response.Write(@"<script>window.opener=null;window.close();</script>");
37 HttpContext.Current.Response.End();
38 }
39
40 /// <summary>
41 /// 给控件增加客户端属性
42
43 /// </summary>
44 /// <param name="control">控件</param>
45 /// <param name="attributeName">属性名</param>
46 /// <param name="attributeValue">属性值</param>
47 /// <example>
48 /// JS.AddAttributesToControl(TextBox1,"oncopy","return false;"); --生成的客户端html中增加 oncopy="return false;"
49 /// </example>
50 public static void AddAttributesToControl(IAttributeAccessor control, string attributeName, string attributeValue)
51 {
52 control.SetAttribute(attributeName, attributeValue);
53 }
54
55 /// <summary>
56 /// 添加要执行的脚本
57 /// </summary>
58 /// <param name="scripts">脚本内容</param>
59 /// <param name="key">脚本的键(如果该键已有则不重复创建)</param>
60 public static void AddScript(string scripts, string key)
61 {
62 StringBuilder sb = new StringBuilder("<script language='javascript'>");
63 sb.Append(scripts);
64 sb.Replace("\\", "\\\\");
65 sb.Replace("\n", "\\n");
66 sb.Replace("\t", "\\t");
67 sb.Replace("\r", "\\r");
68 sb.Append("</script>");
69 CurrPage.ClientScript.RegisterStartupScript(CurrPage.GetType(), key, sb.ToString());
70 }
71
72 /// <summary>
73 /// 打开窗口(不允许一个页面打开多个窗口)
74 /// </summary>
75 /// <param name="page">this</param>
76 /// <param name="URL">地址</param>
77 /// <param name="Target">目标框架</param>
78 public static void open(string url, string target)
79 {
80 AddScript("window.open('" + url + "','','" + target + "');", "open");
81 }
82
83 /// <summary>
84 /// 跳转页面
85 /// </summary>
86 /// <param name="url">地址</param>
87 public static void location(string url)
88 {
89 AddScript("window.location.href='" + url + "';", "redirect");
90 }
91
92 /// <summary>
93 /// 在页面顶部添加脚本
94
95 /// </summary>
96 /// <param name="values">脚本</param>
97 public static void AddHead(string script)
98 {
99 StringBuilder sb = new StringBuilder("<script language=\"javascript\">");
100 sb.Append(script);
101 sb.Append("</script>");
102 CurrPage.Response.Write(sb.ToString());
103 }
104
105 /// <summary>
106 /// 弹出
107 /// </summary>
108 /// <param name="Msg">提示文本</param>
109 public static void alert(string message)
110 {
111 AddScript("alert('" + message + "');", "alert");
112 }
113
114 /// <summary>
115 /// 带有图标的弹出窗口
116
117 /// </summary>
118 /// <param name="Icon">1 信息 2 错误 3警告</param>
119 /// <param name="Message">弹出的信息内容</param>
120 public static void alert(int Icon, string Message)
121 {
122 StringBuilder sb = new StringBuilder();
123 Message = Message.Replace("\"", "\u201c");
124 Message = Message.Replace("'", "\u2018");
125 sb.Append("<script language=vbscript>\n");
126 switch (Icon)
127 {
128 case 1:
129 sb.Append("msgbox \"" + Message + "\",\"64\"\n");
130 break;
131
132 case 2:
133 sb.Append("msgbox \"" + Message + "\",\"16\"\n");
134 break;
135
136 case 3:
137 sb.Append("msgbox \"" + Message + "\",\"48\"\n");
138 break;
139 }
140 sb.Append("</script>");
141 CurrPage.ClientScript.RegisterStartupScript(typeof(string), "ALERT", sb.ToString());
142 }
143
144
145
146
147 /// <summary>
148 /// 弹出提示并跳转页面
149
150 /// </summary>
151 /// <param name="page">this</param>
152 /// <param name="Msg">提示信息</param>
153 /// <param name="URL">跳转地址</param>
154 /// <param name="Target">目标框架</param>
155 public static void AlertAndRedirect(string message, string url, string target)
156 {
157 alert(message);
158 open(url, target);
159 }
160
161 /// <summary>
162 ///先弹出提示框后跳转
163
164 /// </summary>
165 /// <param name="message"></param>
166 /// <param name="url"></param>
167 public static void AlertAndRedirect(string message, string url)
168 {
169 alert(message);
170 location(url);
171 }
172
173 /// <summary>
174 ///先弹出提示框后跳转
175
176 /// </summary>
177 /// <param name="message"></param>
178 /// <param name="url"></param>
179 public static void AlertAndRedirectScroll(string message, string url)
180 {
181 alert(message);
182 AddScript("window.open('" + url + "','null', 'height=700, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');", "redirect");
183 //location(url);
184 }
185
186 /// <summary>
187 ///先弹出提示框后跳转
188
189 /// </summary>
190 /// <param name="message"></param>
191 /// <param name="url"></param>
192 public static void AlertAndRedirectScroll(string url)
193 {
194 AddScript("window.open('" + url + "','null', 'height=700, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');", "redirect");
195 //location(url);
196 }
197 /// <summary>
198 /// 获取客户端IP地址
199 /// </summary>
200 /// <returns></returns>
201 public static string GetClientIP()
202 {
203 return HttpContext.Current.Request.UserHostAddress;
204 }
2 /// 通用的JS脚本
3 /// </summary>
4 public class JSUtils
5 {
6 public JSUtils()
7 {
8 //
9 // TODO: 在此处添加构造函数逻辑
10 //
11 }
12
13 public static Page CurrPage
14 {
15 get
16 {
17 return HttpContext.Current.Handler as Page;
18 }
19 }
20 /// <summary>
21 /// 向一个控件添加一个确认对话框.(如果取消则不提交服务器端)
22 /// </summary>
23 /// <param name="control">需要添加确认的控件</param>
24 /// <param name="message">对话框中显示的文本</param>
25 public static void AddConfirmToControl(IAttributeAccessor control, string message)
26 {
27 message = message.Replace("\\", "\\\\").Replace("\n", "\\n").Replace("\'", "\\'");
28 control.SetAttribute("onclick", "return confirm('" + message + "');");
29 }
30
31 /// <summary>
32 /// 直接关闭页面(忽略其他提示)
33 /// </summary>
34 public static void ClosePage()
35 {
36 HttpContext.Current.Response.Write(@"<script>window.opener=null;window.close();</script>");
37 HttpContext.Current.Response.End();
38 }
39
40 /// <summary>
41 /// 给控件增加客户端属性
42
43 /// </summary>
44 /// <param name="control">控件</param>
45 /// <param name="attributeName">属性名</param>
46 /// <param name="attributeValue">属性值</param>
47 /// <example>
48 /// JS.AddAttributesToControl(TextBox1,"oncopy","return false;"); --生成的客户端html中增加 oncopy="return false;"
49 /// </example>
50 public static void AddAttributesToControl(IAttributeAccessor control, string attributeName, string attributeValue)
51 {
52 control.SetAttribute(attributeName, attributeValue);
53 }
54
55 /// <summary>
56 /// 添加要执行的脚本
57 /// </summary>
58 /// <param name="scripts">脚本内容</param>
59 /// <param name="key">脚本的键(如果该键已有则不重复创建)</param>
60 public static void AddScript(string scripts, string key)
61 {
62 StringBuilder sb = new StringBuilder("<script language='javascript'>");
63 sb.Append(scripts);
64 sb.Replace("\\", "\\\\");
65 sb.Replace("\n", "\\n");
66 sb.Replace("\t", "\\t");
67 sb.Replace("\r", "\\r");
68 sb.Append("</script>");
69 CurrPage.ClientScript.RegisterStartupScript(CurrPage.GetType(), key, sb.ToString());
70 }
71
72 /// <summary>
73 /// 打开窗口(不允许一个页面打开多个窗口)
74 /// </summary>
75 /// <param name="page">this</param>
76 /// <param name="URL">地址</param>
77 /// <param name="Target">目标框架</param>
78 public static void open(string url, string target)
79 {
80 AddScript("window.open('" + url + "','','" + target + "');", "open");
81 }
82
83 /// <summary>
84 /// 跳转页面
85 /// </summary>
86 /// <param name="url">地址</param>
87 public static void location(string url)
88 {
89 AddScript("window.location.href='" + url + "';", "redirect");
90 }
91
92 /// <summary>
93 /// 在页面顶部添加脚本
94
95 /// </summary>
96 /// <param name="values">脚本</param>
97 public static void AddHead(string script)
98 {
99 StringBuilder sb = new StringBuilder("<script language=\"javascript\">");
100 sb.Append(script);
101 sb.Append("</script>");
102 CurrPage.Response.Write(sb.ToString());
103 }
104
105 /// <summary>
106 /// 弹出
107 /// </summary>
108 /// <param name="Msg">提示文本</param>
109 public static void alert(string message)
110 {
111 AddScript("alert('" + message + "');", "alert");
112 }
113
114 /// <summary>
115 /// 带有图标的弹出窗口
116
117 /// </summary>
118 /// <param name="Icon">1 信息 2 错误 3警告</param>
119 /// <param name="Message">弹出的信息内容</param>
120 public static void alert(int Icon, string Message)
121 {
122 StringBuilder sb = new StringBuilder();
123 Message = Message.Replace("\"", "\u201c");
124 Message = Message.Replace("'", "\u2018");
125 sb.Append("<script language=vbscript>\n");
126 switch (Icon)
127 {
128 case 1:
129 sb.Append("msgbox \"" + Message + "\",\"64\"\n");
130 break;
131
132 case 2:
133 sb.Append("msgbox \"" + Message + "\",\"16\"\n");
134 break;
135
136 case 3:
137 sb.Append("msgbox \"" + Message + "\",\"48\"\n");
138 break;
139 }
140 sb.Append("</script>");
141 CurrPage.ClientScript.RegisterStartupScript(typeof(string), "ALERT", sb.ToString());
142 }
143
144
145
146
147 /// <summary>
148 /// 弹出提示并跳转页面
149
150 /// </summary>
151 /// <param name="page">this</param>
152 /// <param name="Msg">提示信息</param>
153 /// <param name="URL">跳转地址</param>
154 /// <param name="Target">目标框架</param>
155 public static void AlertAndRedirect(string message, string url, string target)
156 {
157 alert(message);
158 open(url, target);
159 }
160
161 /// <summary>
162 ///先弹出提示框后跳转
163
164 /// </summary>
165 /// <param name="message"></param>
166 /// <param name="url"></param>
167 public static void AlertAndRedirect(string message, string url)
168 {
169 alert(message);
170 location(url);
171 }
172
173 /// <summary>
174 ///先弹出提示框后跳转
175
176 /// </summary>
177 /// <param name="message"></param>
178 /// <param name="url"></param>
179 public static void AlertAndRedirectScroll(string message, string url)
180 {
181 alert(message);
182 AddScript("window.open('" + url + "','null', 'height=700, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');", "redirect");
183 //location(url);
184 }
185
186 /// <summary>
187 ///先弹出提示框后跳转
188
189 /// </summary>
190 /// <param name="message"></param>
191 /// <param name="url"></param>
192 public static void AlertAndRedirectScroll(string url)
193 {
194 AddScript("window.open('" + url + "','null', 'height=700, width=1000, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');", "redirect");
195 //location(url);
196 }
197 /// <summary>
198 /// 获取客户端IP地址
199 /// </summary>
200 /// <returns></returns>
201 public static string GetClientIP()
202 {
203 return HttpContext.Current.Request.UserHostAddress;
204 }
作者:蓝之风
出处:http://www.cnblogs.com/vaiyanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。