forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptHelper.cs
More file actions
359 lines (330 loc) · 13 KB
/
JavaScriptHelper.cs
File metadata and controls
359 lines (330 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HD.Helper.Common
{
/// <summary>
/// JavaScript客户端脚本输出帮助类
/// </summary>
public class JavaScriptHelper
{
#region 弹出信息,并跳转指定页面
/// <summary>
/// 弹出信息,并跳转指定页面。
/// </summary>
public static void AlertAndRedirect(string message, string toURL)
{
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
HttpContext.Current.Response.End();
}
#endregion
#region 弹出信息,并返回历史页面
/// <summary>
/// 弹出信息,并返回历史页面
/// </summary>
public static void AlertAndGoHistory(string message, int value)
{
string js = @"<Script language='JavaScript'>alert('{0}');history.go({1});</Script>";
HttpContext.Current.Response.Write(string.Format(js, message, value));
HttpContext.Current.Response.End();
}
#endregion
#region 弹出信息 并指定到父窗口
/// <summary>
/// 弹出信息 并指定到父窗口
/// </summary>
public static void AlertAndParentUrl(string message, string toURL)
{
string js = "<script language=javascript>alert('{0}');window.top.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
}
#endregion
#region 返回到父窗口
/// <summary>
/// 返回到父窗口
/// </summary>
public static void ParentRedirect(string ToUrl)
{
string js = "<script language=javascript>window.top.location.replace('{0}')</script>";
HttpContext.Current.Response.Write(string.Format(js, ToUrl));
}
#endregion
#region 弹出信息
/// <summary>
/// 弹出信息
/// </summary>
public static void Alert(string message)
{
string js = "<script language=javascript>alert('{0}');</script>";
HttpContext.Current.Response.Write(string.Format(js, message));
}
#endregion
#region 注册脚本块
/// <summary>
/// 注册脚本块
/// </summary>
public static void RegisterScriptBlock(System.Web.UI.Page page, string _ScriptString)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "scriptblock", "<script type='text/javascript'>" + _ScriptString + "</script>");
}
#endregion
#region 显示模态窗口
/// <summary>
/// 返回把指定链接地址显示模态窗口的脚本
/// </summary>
/// <param name="wid"></param>
/// <param name="title"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="url"></param>
public static string GetShowModalWindowScript(string wid, string title, int width, int height, string url)
{
return string.Format("setTimeout(\"showModalWindow('{0}','{1}',{2},{3},'{4}')\",100);", wid, title, width, height, url);
}
/// <summary>
/// 把指定链接地址显示模态窗口
/// </summary>
/// <param name="wid">窗口ID</param>
/// <param name="title">标题</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="url">链接地址</param>
public static void ShowModalWindow(string wid, string title, int width, int height, string url)
{
WriteScript(GetShowModalWindowScript(wid, title, width, height, url));
}
/// <summary>
/// 为指定控件绑定前台脚本:显示模态窗口
/// </summary>
/// <param name="control"></param>
/// <param name="eventName"></param>
/// <param name="wid"></param>
/// <param name="title"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="url"></param>
/// <param name="isScriptEnd"></param>
public static void ShowCilentModalWindow(string wid, WebControl control, string eventName, string title, int width, int height, string url, bool isScriptEnd)
{
string script = isScriptEnd ? "return false;" : "";
control.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);
}
/// <summary>
/// 为指定控件绑定前台脚本:显示模态窗口
/// </summary>
/// <param name="cell"></param>
/// <param name="eventName"></param>
/// <param name="wid"></param>
/// <param name="title"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="url"></param>
/// <param name="isScriptEnd"></param>
public static void ShowCilentModalWindow(string wid, TableCell cell, string eventName, string title, int width, int height, string url, bool isScriptEnd)
{
string script = isScriptEnd ? "return false;" : "";
cell.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);
}
#endregion
#region 显示客户端确认窗口
/// <summary>
/// 显示客户端确认窗口
/// </summary>
/// <param name="control"></param>
/// <param name="eventName"></param>
/// <param name="message"></param>
public static void ShowCilentConfirm(WebControl control, string eventName, string message)
{
ShowCilentConfirm(control, eventName, "系统提示", 210, 125, message);
}
/// <summary>
/// 显示客户端确认窗口
/// </summary>
/// <param name="control"></param>
/// <param name="eventName"></param>
/// <param name="title"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="message"></param>
public static void ShowCilentConfirm(WebControl control, string eventName, string title, int width, int height, string message)
{
control.Attributes[eventName] = string.Format("return showConfirm('{0}',{1},{2},'{3}','{4}');", title, width, height, message, control.ClientID);
}
#endregion
#region 写javascript脚本
/// <summary>
/// 写javascript脚本
/// </summary>
/// <param name="script">脚本内容</param>
public static void WriteScript(string script)
{
Page page = GetCurrentPage();
page.ClientScript.RegisterStartupScript(
page.GetType(),
System.Guid.NewGuid().ToString(),
script,
true);
}
/// <summary>
/// 得到当前页对象实例
/// </summary>
/// <returns></returns>
public static Page GetCurrentPage()
{
return (Page)HttpContext.Current.Handler;
}
#endregion
#region 回到历史页面
/// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
public static void GoHistory(int value)
{
#region
string js = @"<Script language='JavaScript'>
history.go({0});
</Script>";
HttpContext.Current.Response.Write(string.Format(js, value));
HttpContext.Current.Response.End();
#endregion
}
#endregion
#region 关闭当前窗口
/// <summary>
/// 关闭当前窗口
/// </summary>
public static void CloseWindow()
{
#region
string js = @"<Script language='JavaScript'>
parent.opener=null;window.close();
</Script>";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
#endregion
}
#endregion
#region 刷新父窗口
/// <summary>
/// 刷新父窗口
/// </summary>
public static void RefreshParent(string url)
{
#region
string js = @"<script>try{top.location=""" + url + @"""}catch(e){location=""" + url + @"""}</script>";
HttpContext.Current.Response.Write(js);
#endregion
}
#endregion
#region 刷新打开窗口
/// <summary>
/// 刷新打开窗口
/// </summary>
public static void RefreshOpener()
{
#region
string js = @"<Script language='JavaScript'>
opener.location.reload();
</Script>";
HttpContext.Current.Response.Write(js);
#endregion
}
#endregion
#region 转向Url指定的页面
/// <summary>
/// 转向Url指定的页面
/// </summary>
/// <param name="url">连接地址</param>
public static void JavaScriptLocationHref(string url)
{
#region
string js = @"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
HttpContext.Current.Response.Write(js);
#endregion
}
#endregion
#region 打开指定大小位置的模式对话框
/// <summary>
/// 打开指定大小位置的模式对话框
/// </summary>
/// <param name="webFormUrl">连接地址</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="top">距离上位置</param>
/// <param name="left">距离左位置</param>
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
{
#region
string features = "dialogWidth:" + width.ToString() + "px"
+ ";dialogHeight:" + height.ToString() + "px"
+ ";dialogLeft:" + left.ToString() + "px"
+ ";dialogTop:" + top.ToString() + "px"
+ ";center:yes;help=no;resizable:no;status:no;scroll=yes";
ShowModalDialogWindow(webFormUrl, features);
#endregion
}
#endregion
#region 打开模式对话框
/// <summary>
/// 打开模式对话框
/// </summary>
/// <param name="webFormUrl">链接地址</param>
/// <param name="features"></param>
public static void ShowModalDialogWindow(string webFormUrl, string features)
{
string js = ShowModalDialogJavascript(webFormUrl, features);
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 打开模式对话框
/// </summary>
/// <param name="webFormUrl"></param>
/// <param name="features"></param>
/// <returns></returns>
public static string ShowModalDialogJavascript(string webFormUrl, string features)
{
#region
string js = @"<script language=javascript>
showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
return js;
#endregion
}
#endregion
#region 打开指定大小的新窗体
/// <summary>
/// 打开指定大小的新窗体
/// </summary>
/// <param name="url">地址</param>
/// <param name="width">宽</param>
/// <param name="heigth">高</param>
/// <param name="top">头位置</param>
/// <param name="left">左位置</param>
public static void OpenWebFormSize(string url, int width, int heigth, int top, int left)
{
#region
string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";
HttpContext.Current.Response.Write(js);
#endregion
}
#endregion
#region 页面跳转(跳出框架)
/// <summary>
/// 页面跳转(跳出框架)
/// </summary>
/// <param name="url"></param>
public static void JavaScriptExitIfream(string url)
{
string js = @"<Script language='JavaScript'>
parent.window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
HttpContext.Current.Response.Write(js);
}
#endregion
}
}