ASP.NET 常用到的一些通用函数
Code
1public abstract class JS
2 2 {
3 3 /**//**//**//// <summary>
4 4 /**//// 客户端打开窗口
5 5 /**//// </summary>
6 6 /**//// <param name="strUrl"></param>
7 7 public static void OpenWindow(string strUrl)
8 8 {
9 9 HttpContext.Current.Response.Write("<script language=javascript>");
10 10 HttpContext.Current.Response.Write("window.open('" + strUrl + "');");
11 11 HttpContext.Current.Response.Write("</script>");
12 12
13 13 }
14 14 /**//**//**//// <summary>
15 15 /**//// 输出自定义脚本信息
16 16 /**//// </summary>
17 17 /**//// <param name="page">当前页面指针,一般为this</param>
18 18 /**//// <param name="script">输出脚本</param>
19 19 public static void ResponseScript(System.Web.UI.Page page,string script)
20 20 {
21 21 page.RegisterStartupScript("message","<script language='javascript' defer>"+script+"</script>");
22 22 }
23 23 /**//**//**//// <summary>
24 24 /**//// 显示消息提示对话框,并进行页面跳转
25 25 /**//// </summary>
26 26 /**//// <param name="page">当前页面指针,一般为this</param>
27 27 /**//// <param name="msg">提示信息</param>
28 28 /**//// <param name="url">跳转的目标URL</param>
29 29 public static void ShowAndRedirect(System.Web.UI.Page page,string msg,string url)
30 30 {
31 31 StringBuilder Builder=new StringBuilder();
32 32 Builder.Append("<script language='javascript' defer>");
33 33 Builder.AppendFormat("alert('{0}');",msg);
34 34 Builder.AppendFormat("top.location.href='{0}'",url);
35 35 Builder.Append("</script>");
36 36 page.RegisterStartupScript("message",Builder.ToString());
37 37 }
38 38
39 39
40 40 public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control,string msg)
41 41 {
42 42 Control.Attributes.Add("onclick", "return confirm('" + msg + "');") ;
43 43 }
44 44 /**//**//**//// <summary>
45 45 /**//// 清空所有panel里面的textbox内容
46 46 /**//// </summary>
47 47 /**//// <param name="pan"></param>
48 48 public static void clearAll(System.Web.UI.WebControls.Panel pan)
49 49 {
50 50 foreach (Control panl in pan.Controls )
51 51 {
52 52 if(panl.GetType().ToString()=="System.Web.UI.WebControls.TextBox")
53 53 {
54 54 ((System.Web.UI.WebControls.TextBox)panl).Text="";
55 55 }
56 56 }
57 57 }
58 58
59 59 /**//**//**//// <summary>
60 60 /**//// 窗体加载以后探出对话框
61 61 /**//// </summary>
62 62 public static void Alert(string msg)
63 63 {
64 64 Page pages;
65 65 pages= HttpContext.Current.Handler as System.Web.UI.Page;
66 66 msg=msg.Replace("'","");
67 67 msg=msg.Replace("\"","");
68 68 msg=msg.Replace("\n",@"\n").Replace("\r",@"\r").Replace("\"",@"\""") ;
69 69
70 70 pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>alert('" + msg + "');</script>"));
71 71 }
72 72 /**//**//**//// <summary>
73 73 /**//// 窗体没有加载的时候如pageload的时候探出对话框
74 74 /**//// </summary>
75 75 public static void Alert_none(string msg)
76 76 {
77 77 Page pages;
78 78 pages= HttpContext.Current.Handler as System.Web.UI.Page;
79 79 msg=msg.Replace("'","");
80 80 msg=msg.Replace("\"","");
81 81 msg=msg.Replace("\n",@"\n").Replace("\r",@"\r").Replace("\"",@"\""") ;
82 82 string retu = " alert('" + msg + "');";
83 83 ClientWrite2( retu);
84 84 }
85 85 /**//**//**//// <summary>
86 86 /**//// 加载以后写自己的脚本
87 87 /**//// </summary>
88 88 /**//// <param name="pages"></param>
89 89 /**//// <param name="yourJs"></param>
90 90 public static void ClientWrite(string yourJs)
91 91 {
92 92 Page pages;
93 93 pages= HttpContext.Current.Handler as System.Web.UI.Page;
94 94 pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>" + yourJs + "</script>"));
95 95 }
96 96 /**//**//**//// <summary>
97 97 /**//// 加载以前写自己的脚本
98 98 /**//// </summary>
99 99 /**//// <param name="pages"></param>
100100 /**//// <param name="yourJs"></param>
101101 public static void ClientWrite2(string yourJs)
102102 {
103103 Page pages;
104104 pages= HttpContext.Current.Handler as System.Web.UI.Page;
105105 pages.Response.Write("<script language=javascript>");
106106 pages.Response.Write(yourJs );
107107 pages.Response.Write(" </script>");
108108 }
109109
110110 /**//**//**//// <summary>
111111 /**//// 得到刷新界面的字符串
112112 /**//// </summary>
113113 /**//// <returns></returns>
114114 public static string RefreshWin()
115115 {
116116 return "window.location=window.location.href;";
117117 }
118118 /**//**//**//// <summary>
119119 /**//// 打开小窗体
120120 /**//// </summary>
121121 /**//// <param name="url"></param>
122122 /**//// <param name="width"></param>
123123 /**//// <param name="height"></param>
124124 /**//// <param name="top"></param>
125125 /**//// <param name="left"></param>
126126 public static void OpenLittleWindow(string url,int width,int height,int top,int left)
127127 {
128128 string str;
129129 str = "javascript:var popup;popup=window.open('{url}',null,'scrollbars=yes,status=no,width={width},height={height},top={top},left={left}');popup.opener=self.opener;self.close();";
130130 str = str.Replace("{width}", width.ToString());
131131 str = str.Replace("{height}", height.ToString());
132132 str = str.Replace("{top}", top.ToString());
133133 str = str.Replace("{left}", left.ToString());
134134 str=str.Replace("{url}",url);
135135 }
136136
137137 /**//**//**//// <summary>
138138 /**//// 回车-〉tab
139139 /**//// </summary>
140140 /**//// <param name="page"></param>
141141 public static void ToTab()
142142 {
143143 Page page;
144144 page= HttpContext.Current.Handler as System.Web.UI.Page;
145145 System.Text.StringBuilder scriptFunction=new StringBuilder();
146146 scriptFunction.Append("<script language='javascript'>");
147147 scriptFunction.Append(" function returnTotab()");
148148 scriptFunction.Append(" {");
149149 scriptFunction.Append(" if(event.keyCode==13) ");
150150 scriptFunction.Append(" {event.keyCode=9; ");
151151 scriptFunction.Append(" return true;} ");
152152 scriptFunction.Append(" } ");
153153 scriptFunction.Append("</script>");
154154 page.RegisterStartupScript("totab", scriptFunction.ToString());
155155
156156 }
157157 /**//**//**//// <summary>
158158 /**//// tab->enter
159159 /**//// </summary>
160160 /**//// <param name="page"></param>
161161 public static void tabToEnter()
162162 {
163163 Page page;
164164 page= HttpContext.Current.Handler as System.Web.UI.Page;
165165 System.Text.StringBuilder scriptFunction=new StringBuilder();
166166 scriptFunction.Append("<script language='javascript'>");
167167 scriptFunction.Append(" function Tcheck()");
168168 scriptFunction.Append(" {");
169169 scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
170170 scriptFunction.Append(" return true;");
171171 scriptFunction.Append(" else ");
172172 scriptFunction.Append(" {");
173173 scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
174174 scriptFunction.Append(" return true;");
175175 scriptFunction.Append(" else");
176176 scriptFunction.Append(" if(event.keyCode==13||event.keyCode==110||event.keyCode==190||event.keyCode==39)");
177177 scriptFunction.Append(" {event.keyCode=9;");
178178 scriptFunction.Append(" return true;}");
179179 scriptFunction.Append(" else");
180180 scriptFunction.Append(" return false;");
181181 scriptFunction.Append(" }");
182182 scriptFunction.Append(" } ");
183183 scriptFunction.Append("</script>");
184184 page.RegisterStartupScript("switch", scriptFunction.ToString());
185185 }
186186 /**//**//**//// <summary>
187187 /**//// attachEvent
188188 /**//// </summary>
189189 /**//// <param name="controlToFocus"></param>
190190 /**//// <param name="page"></param>
191191 public static void attachEvent(Control[] controlToFocus)
192192 {
193193 Page page;
194194 page= HttpContext.Current.Handler as System.Web.UI.Page;
195195 System.Text.StringBuilder scriptFunction=new StringBuilder();
196196 string scriptClientId;
197197 scriptFunction.Append("<script language='javascript'>");
198198
199199 foreach(Control con in controlToFocus)
200200 {
201201 scriptClientId = con.ClientID;
202202 scriptFunction.Append("document.getElementById('" + scriptClientId + "').attachEvent('onkeydown', Tcheck);");
203203 }
204204 scriptFunction.Append("</script>");
205205 page.RegisterStartupScript("attach", scriptFunction.ToString());
206206 }
207207 /**//**//**//// <summary>
208208 /**////
209209 /**//// </summary>
210210 /**//// <param name="controlToFocus"></param>
211211 /**//// <param name="page"></param>
212212 /**//// <param name="eventStr"></param>
213213 /**//// <param name="FuncStr"></param>
214214 public static void AttachEvent(Control[] controlToFocus,string eventStr,string FuncStr)
215215 {
216216 Page page;
217217 page= HttpContext.Current.Handler as System.Web.UI.Page;
218218 System.Text.StringBuilder scriptFunction=new StringBuilder();
219219 string scriptClientId;
220220 scriptFunction.Append("<script language='javascript'>");
221221 foreach(Control con in controlToFocus)
222222 {
223223 scriptClientId = con.ClientID;
224224 scriptFunction.Append("document.getElementById('" + scriptClientId + "').attachEvent('" + eventStr + "', " + FuncStr + ");");
225225 }
226226 scriptFunction.Append("</script>");
227227 page.RegisterStartupScript("attach2", scriptFunction.ToString());
228228 }
229229 /**//**//**//// <summary>
230230 /**////
231231 /**//// </summary>
232232 /**//// <param name="page"></param>
233233 public static void NumOnlyFun()
234234 {
235235 Page page;
236236 page= HttpContext.Current.Handler as System.Web.UI.Page;
237237 System.Text.StringBuilder scriptFunction=new StringBuilder();
238238 scriptFunction.Append("<script language='javascript'>");
239239 scriptFunction.Append(" function isNum()");
240240 scriptFunction.Append(" {");
241241 scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
242242 scriptFunction.Append(" return true;");
243243 scriptFunction.Append(" else ");
244244 scriptFunction.Append(" {");
245245 scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
246246 scriptFunction.Append(" return true;");
247247 scriptFunction.Append(" else");
248248 scriptFunction.Append(" return false;");
249249 scriptFunction.Append(" }");
250250 scriptFunction.Append(" } ");
251251 scriptFunction.Append("</script>");
252252 page.RegisterStartupScript("numonly", scriptFunction.ToString());
253253 }
254254 }
1public abstract class JS
2 2 {
3 3 /**//**//**//// <summary>
4 4 /**//// 客户端打开窗口
5 5 /**//// </summary>
6 6 /**//// <param name="strUrl"></param>
7 7 public static void OpenWindow(string strUrl)
8 8 {
9 9 HttpContext.Current.Response.Write("<script language=javascript>");
10 10 HttpContext.Current.Response.Write("window.open('" + strUrl + "');");
11 11 HttpContext.Current.Response.Write("</script>");
12 12
13 13 }
14 14 /**//**//**//// <summary>
15 15 /**//// 输出自定义脚本信息
16 16 /**//// </summary>
17 17 /**//// <param name="page">当前页面指针,一般为this</param>
18 18 /**//// <param name="script">输出脚本</param>
19 19 public static void ResponseScript(System.Web.UI.Page page,string script)
20 20 {
21 21 page.RegisterStartupScript("message","<script language='javascript' defer>"+script+"</script>");
22 22 }
23 23 /**//**//**//// <summary>
24 24 /**//// 显示消息提示对话框,并进行页面跳转
25 25 /**//// </summary>
26 26 /**//// <param name="page">当前页面指针,一般为this</param>
27 27 /**//// <param name="msg">提示信息</param>
28 28 /**//// <param name="url">跳转的目标URL</param>
29 29 public static void ShowAndRedirect(System.Web.UI.Page page,string msg,string url)
30 30 {
31 31 StringBuilder Builder=new StringBuilder();
32 32 Builder.Append("<script language='javascript' defer>");
33 33 Builder.AppendFormat("alert('{0}');",msg);
34 34 Builder.AppendFormat("top.location.href='{0}'",url);
35 35 Builder.Append("</script>");
36 36 page.RegisterStartupScript("message",Builder.ToString());
37 37 }
38 38
39 39
40 40 public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control,string msg)
41 41 {
42 42 Control.Attributes.Add("onclick", "return confirm('" + msg + "');") ;
43 43 }
44 44 /**//**//**//// <summary>
45 45 /**//// 清空所有panel里面的textbox内容
46 46 /**//// </summary>
47 47 /**//// <param name="pan"></param>
48 48 public static void clearAll(System.Web.UI.WebControls.Panel pan)
49 49 {
50 50 foreach (Control panl in pan.Controls )
51 51 {
52 52 if(panl.GetType().ToString()=="System.Web.UI.WebControls.TextBox")
53 53 {
54 54 ((System.Web.UI.WebControls.TextBox)panl).Text="";
55 55 }
56 56 }
57 57 }
58 58
59 59 /**//**//**//// <summary>
60 60 /**//// 窗体加载以后探出对话框
61 61 /**//// </summary>
62 62 public static void Alert(string msg)
63 63 {
64 64 Page pages;
65 65 pages= HttpContext.Current.Handler as System.Web.UI.Page;
66 66 msg=msg.Replace("'","");
67 67 msg=msg.Replace("\"","");
68 68 msg=msg.Replace("\n",@"\n").Replace("\r",@"\r").Replace("\"",@"\""") ;
69 69
70 70 pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>alert('" + msg + "');</script>"));
71 71 }
72 72 /**//**//**//// <summary>
73 73 /**//// 窗体没有加载的时候如pageload的时候探出对话框
74 74 /**//// </summary>
75 75 public static void Alert_none(string msg)
76 76 {
77 77 Page pages;
78 78 pages= HttpContext.Current.Handler as System.Web.UI.Page;
79 79 msg=msg.Replace("'","");
80 80 msg=msg.Replace("\"","");
81 81 msg=msg.Replace("\n",@"\n").Replace("\r",@"\r").Replace("\"",@"\""") ;
82 82 string retu = " alert('" + msg + "');";
83 83 ClientWrite2( retu);
84 84 }
85 85 /**//**//**//// <summary>
86 86 /**//// 加载以后写自己的脚本
87 87 /**//// </summary>
88 88 /**//// <param name="pages"></param>
89 89 /**//// <param name="yourJs"></param>
90 90 public static void ClientWrite(string yourJs)
91 91 {
92 92 Page pages;
93 93 pages= HttpContext.Current.Handler as System.Web.UI.Page;
94 94 pages.Controls.Add(new System.Web.UI.LiteralControl("<script language=javascript>" + yourJs + "</script>"));
95 95 }
96 96 /**//**//**//// <summary>
97 97 /**//// 加载以前写自己的脚本
98 98 /**//// </summary>
99 99 /**//// <param name="pages"></param>
100100 /**//// <param name="yourJs"></param>
101101 public static void ClientWrite2(string yourJs)
102102 {
103103 Page pages;
104104 pages= HttpContext.Current.Handler as System.Web.UI.Page;
105105 pages.Response.Write("<script language=javascript>");
106106 pages.Response.Write(yourJs );
107107 pages.Response.Write(" </script>");
108108 }
109109
110110 /**//**//**//// <summary>
111111 /**//// 得到刷新界面的字符串
112112 /**//// </summary>
113113 /**//// <returns></returns>
114114 public static string RefreshWin()
115115 {
116116 return "window.location=window.location.href;";
117117 }
118118 /**//**//**//// <summary>
119119 /**//// 打开小窗体
120120 /**//// </summary>
121121 /**//// <param name="url"></param>
122122 /**//// <param name="width"></param>
123123 /**//// <param name="height"></param>
124124 /**//// <param name="top"></param>
125125 /**//// <param name="left"></param>
126126 public static void OpenLittleWindow(string url,int width,int height,int top,int left)
127127 {
128128 string str;
129129 str = "javascript:var popup;popup=window.open('{url}',null,'scrollbars=yes,status=no,width={width},height={height},top={top},left={left}');popup.opener=self.opener;self.close();";
130130 str = str.Replace("{width}", width.ToString());
131131 str = str.Replace("{height}", height.ToString());
132132 str = str.Replace("{top}", top.ToString());
133133 str = str.Replace("{left}", left.ToString());
134134 str=str.Replace("{url}",url);
135135 }
136136
137137 /**//**//**//// <summary>
138138 /**//// 回车-〉tab
139139 /**//// </summary>
140140 /**//// <param name="page"></param>
141141 public static void ToTab()
142142 {
143143 Page page;
144144 page= HttpContext.Current.Handler as System.Web.UI.Page;
145145 System.Text.StringBuilder scriptFunction=new StringBuilder();
146146 scriptFunction.Append("<script language='javascript'>");
147147 scriptFunction.Append(" function returnTotab()");
148148 scriptFunction.Append(" {");
149149 scriptFunction.Append(" if(event.keyCode==13) ");
150150 scriptFunction.Append(" {event.keyCode=9; ");
151151 scriptFunction.Append(" return true;} ");
152152 scriptFunction.Append(" } ");
153153 scriptFunction.Append("</script>");
154154 page.RegisterStartupScript("totab", scriptFunction.ToString());
155155
156156 }
157157 /**//**//**//// <summary>
158158 /**//// tab->enter
159159 /**//// </summary>
160160 /**//// <param name="page"></param>
161161 public static void tabToEnter()
162162 {
163163 Page page;
164164 page= HttpContext.Current.Handler as System.Web.UI.Page;
165165 System.Text.StringBuilder scriptFunction=new StringBuilder();
166166 scriptFunction.Append("<script language='javascript'>");
167167 scriptFunction.Append(" function Tcheck()");
168168 scriptFunction.Append(" {");
169169 scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
170170 scriptFunction.Append(" return true;");
171171 scriptFunction.Append(" else ");
172172 scriptFunction.Append(" {");
173173 scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
174174 scriptFunction.Append(" return true;");
175175 scriptFunction.Append(" else");
176176 scriptFunction.Append(" if(event.keyCode==13||event.keyCode==110||event.keyCode==190||event.keyCode==39)");
177177 scriptFunction.Append(" {event.keyCode=9;");
178178 scriptFunction.Append(" return true;}");
179179 scriptFunction.Append(" else");
180180 scriptFunction.Append(" return false;");
181181 scriptFunction.Append(" }");
182182 scriptFunction.Append(" } ");
183183 scriptFunction.Append("</script>");
184184 page.RegisterStartupScript("switch", scriptFunction.ToString());
185185 }
186186 /**//**//**//// <summary>
187187 /**//// attachEvent
188188 /**//// </summary>
189189 /**//// <param name="controlToFocus"></param>
190190 /**//// <param name="page"></param>
191191 public static void attachEvent(Control[] controlToFocus)
192192 {
193193 Page page;
194194 page= HttpContext.Current.Handler as System.Web.UI.Page;
195195 System.Text.StringBuilder scriptFunction=new StringBuilder();
196196 string scriptClientId;
197197 scriptFunction.Append("<script language='javascript'>");
198198
199199 foreach(Control con in controlToFocus)
200200 {
201201 scriptClientId = con.ClientID;
202202 scriptFunction.Append("document.getElementById('" + scriptClientId + "').attachEvent('onkeydown', Tcheck);");
203203 }
204204 scriptFunction.Append("</script>");
205205 page.RegisterStartupScript("attach", scriptFunction.ToString());
206206 }
207207 /**//**//**//// <summary>
208208 /**////
209209 /**//// </summary>
210210 /**//// <param name="controlToFocus"></param>
211211 /**//// <param name="page"></param>
212212 /**//// <param name="eventStr"></param>
213213 /**//// <param name="FuncStr"></param>
214214 public static void AttachEvent(Control[] controlToFocus,string eventStr,string FuncStr)
215215 {
216216 Page page;
217217 page= HttpContext.Current.Handler as System.Web.UI.Page;
218218 System.Text.StringBuilder scriptFunction=new StringBuilder();
219219 string scriptClientId;
220220 scriptFunction.Append("<script language='javascript'>");
221221 foreach(Control con in controlToFocus)
222222 {
223223 scriptClientId = con.ClientID;
224224 scriptFunction.Append("document.getElementById('" + scriptClientId + "').attachEvent('" + eventStr + "', " + FuncStr + ");");
225225 }
226226 scriptFunction.Append("</script>");
227227 page.RegisterStartupScript("attach2", scriptFunction.ToString());
228228 }
229229 /**//**//**//// <summary>
230230 /**////
231231 /**//// </summary>
232232 /**//// <param name="page"></param>
233233 public static void NumOnlyFun()
234234 {
235235 Page page;
236236 page= HttpContext.Current.Handler as System.Web.UI.Page;
237237 System.Text.StringBuilder scriptFunction=new StringBuilder();
238238 scriptFunction.Append("<script language='javascript'>");
239239 scriptFunction.Append(" function isNum()");
240240 scriptFunction.Append(" {");
241241 scriptFunction.Append(" if(event.keyCode==8||event.keyCode==9) ");
242242 scriptFunction.Append(" return true;");
243243 scriptFunction.Append(" else ");
244244 scriptFunction.Append(" {");
245245 scriptFunction.Append(" if(((event.keyCode>=48)++(event.keyCode<=57))||((event.keyCode>=96)++(event.keyCode<=105)))");
246246 scriptFunction.Append(" return true;");
247247 scriptFunction.Append(" else");
248248 scriptFunction.Append(" return false;");
249249 scriptFunction.Append(" }");
250250 scriptFunction.Append(" } ");
251251 scriptFunction.Append("</script>");
252252 page.RegisterStartupScript("numonly", scriptFunction.ToString());
253253 }
254254 }