Brak opisu

JScript.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. namespace XYFDRQ.Common
  7. {
  8. /// <summary>
  9. /// 提供向页面输出客户端代码实现特殊功能的方法
  10. /// </summary>
  11. /// <remarks>
  12. /// </remarks>
  13. public class JScript
  14. {
  15. public static void AlertAndRedirect(string message, string toURL)
  16. {
  17. string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
  18. HttpContext.Current.Response.Write(string.Format(js, message, toURL));
  19. }
  20. public static void AlertAndReturnValueToParent(string message, string jsStr)
  21. {
  22. string js = "<script language=javascript>alert('{0}');{1}</script>";
  23. HttpContext.Current.Response.Write(string.Format(js, message, jsStr));
  24. }
  25. /// <summary>
  26. /// 向客户端发送函数KendoPostBack(eventTarget, eventArgument)
  27. /// 服务器端可接收__EVENTTARGET,__EVENTARGUMENT的值
  28. /// </summary>
  29. /// <param name="page">System.Web.UI.Page 一般为this</param>
  30. public void JscriptSender(System.Web.UI.Page page)
  31. {
  32. page.ClientScript.RegisterHiddenField("__EVENTTARGET", "");
  33. page.ClientScript.RegisterHiddenField("__EVENTARGUMENT", "");
  34. string s = @"
  35. <script language=Javascript>
  36. function KendoPostBack(eventTarget, eventArgument)
  37. {
  38. var theform = document.forms[0];
  39. theform.__EVENTTARGET.value = eventTarget;
  40. theform.__EVENTARGUMENT.value = eventArgument;
  41. theform.submit();
  42. }
  43. </script>";
  44. page.ClientScript.RegisterStartupScript(GetType(), "sds", s);
  45. }
  46. /// <summary>
  47. /// 弹出JavaScript小窗口
  48. /// </summary>
  49. /// <param name="js">窗口信息</param>
  50. public static void Alert(string message)
  51. {
  52. message = StringUtil.DeleteUnVisibleChar(message);
  53. string js = @"<Script language='JavaScript'>
  54. alert('" + message + "');</Script>";
  55. HttpContext.Current.Response.Write(js);
  56. }
  57. public static void Alert(object message)
  58. {
  59. string js = @"<Script language='JavaScript'>
  60. alert('{0}');
  61. </Script>";
  62. HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
  63. }
  64. public static void RtnRltMsgbox(object message, string strWinCtrl)
  65. {
  66. string js = @"<Script language='JavaScript'>
  67. strWinCtrl = true;
  68. strWinCtrl = if(!confirm('" + message + "'))return false;</Script>";
  69. HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
  70. }
  71. /// <summary>
  72. /// 回到历史页面
  73. /// </summary>
  74. /// <param name="value">-1/1</param>
  75. public static void GoHistory(int value)
  76. {
  77. string js = @"<Script language='JavaScript'>
  78. history.go({0});
  79. </Script>";
  80. HttpContext.Current.Response.Write(string.Format(js, value));
  81. }
  82. /// <summary>
  83. /// 关闭当前窗口
  84. /// </summary>
  85. public static void CloseWindow()
  86. {
  87. string js = @"<Script language='JavaScript'>
  88. window.close();
  89. </Script>";
  90. HttpContext.Current.Response.Write(js);
  91. HttpContext.Current.Response.End();
  92. }
  93. /// <summary>
  94. /// 刷新父窗口
  95. /// </summary>
  96. public static void RefreshParent()
  97. {
  98. string js = @"<Script language='JavaScript'>
  99. parent.location.reload();
  100. </Script>";
  101. HttpContext.Current.Response.Write(js);
  102. }
  103. /// <summary>
  104. /// 格式化为JS可解释的字符串
  105. /// </summary>
  106. /// <param name="s"></param>
  107. /// <returns></returns>
  108. public static string JSStringFormat(string s)
  109. {
  110. return s.Replace("\r", "\\r").Replace("\n", "\\n").Replace("'", "\\'").Replace("\"", "\\\"");
  111. }
  112. /// <summary>
  113. /// 刷新打开窗口
  114. /// </summary>
  115. public static void RefreshOpener()
  116. {
  117. string js = @"<Script language='JavaScript'>
  118. opener.location.reload();
  119. </Script>";
  120. HttpContext.Current.Response.Write(js);
  121. }
  122. /// <summary>
  123. /// 打开窗口
  124. /// </summary>
  125. /// <param name="url"></param>
  126. public static void OpenWebForm(string url)
  127. {
  128. /*…………………………………………………………………………………………*/
  129. /*修改人员: sxs */
  130. /*修改时间: 2003-4-9 */
  131. /*修改目的: 新开页面去掉ie的菜单。。。 */
  132. /*注释内容: */
  133. /*开始*/
  134. string js = @"<Script language='JavaScript'>
  135. //window.open('" + url + @"');
  136. window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
  137. </Script>";
  138. /*结束*/
  139. /*…………………………………………………………………………………………*/
  140. HttpContext.Current.Response.Write(js);
  141. }
  142. public static void OpenWebForm(string url, string name, string future)
  143. {
  144. string js = @"<Script language='JavaScript'>
  145. window.open('" + url + @"','" + name + @"','" + future + @"')
  146. </Script>";
  147. HttpContext.Current.Response.Write(js);
  148. }
  149. public static void OpenWebForm(string url, string formName)
  150. {
  151. /*…………………………………………………………………………………………*/
  152. /*修改人员: sxs */
  153. /*修改时间: 2003-4-9 */
  154. /*修改目的: 新开页面去掉ie的菜单。。。 */
  155. /*注释内容: */
  156. /*开始*/
  157. string js = @"<Script language='JavaScript'>
  158. //window.open('" + url + @"','" + formName + @"');
  159. window.open('" + url + @"','" + formName + @"','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
  160. </Script>";
  161. /*结束*/
  162. /*…………………………………………………………………………………………*/
  163. HttpContext.Current.Response.Write(js);
  164. }
  165. /// <summary>
  166. /// 函数名:OpenWebForm
  167. /// 功能描述:打开WEB窗口
  168. /// 处理流程:
  169. /// 算法描述:
  170. /// 作 者: 孙洪彪
  171. /// 日 期: 2003-04-29 17:00
  172. /// 修 改:
  173. /// 日 期:
  174. /// 版 本:
  175. /// </summary>
  176. /// <param name="url">WEB窗口</param>
  177. /// <param name="isFullScreen">是否全屏幕</param>
  178. public static void OpenWebForm(string url, bool isFullScreen)
  179. {
  180. string js = @"<Script language='JavaScript'>";
  181. if (isFullScreen)
  182. {
  183. js += "var iWidth = 0;";
  184. js += "var iHeight = 0;";
  185. js += "iWidth=window.screen.availWidth-10;";
  186. js += "iHeight=window.screen.availHeight-50;";
  187. js += "var szFeatures ='width=' + iWidth + ',height=' + iHeight + ',top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no';";
  188. js += "window.open('" + url + @"','',szFeatures);";
  189. }
  190. else
  191. {
  192. js += "window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');";
  193. }
  194. js += "</Script>";
  195. HttpContext.Current.Response.Write(js);
  196. }
  197. /// <summary>
  198. /// 转向Url制定的页面
  199. /// </summary>
  200. /// <param name="url"></param>
  201. public static void JavaScriptLocationHref(string url)
  202. {
  203. string js = @"<Script language='JavaScript'>
  204. window.location.replace('{0}');
  205. </Script>";
  206. js = string.Format(js, url);
  207. HttpContext.Current.Response.Write(js);
  208. }
  209. /// <summary>
  210. /// 指定的框架页面转换
  211. /// </summary>
  212. /// <param name="FrameName"></param>
  213. /// <param name="url"></param>
  214. public static void JavaScriptFrameHref(string FrameName, string url)
  215. {
  216. string js = @"<Script language='JavaScript'>
  217. @obj.location.replace(""{0}"");
  218. </Script>";
  219. js = js.Replace("@obj", FrameName);
  220. js = string.Format(js, url);
  221. HttpContext.Current.Response.Write(js);
  222. }
  223. /// <summary>
  224. ///重置页面
  225. /// </summary>
  226. public static void JavaScriptResetPage(string strRows)
  227. {
  228. string js = @"<Script language='JavaScript'>
  229. window.parent.CenterFrame.rows='" + strRows + "';</Script>";
  230. HttpContext.Current.Response.Write(js);
  231. }
  232. /// <summary>
  233. /// 函数名:JavaScriptSetCookie
  234. /// 功能描述:客户端方法设置Cookie
  235. /// 作者:sxs
  236. /// 日期:2003-4-9
  237. /// 版本:1.0
  238. /// </summary>
  239. /// <param name="strName">Cookie名</param>
  240. /// <param name="strValue">Cookie值</param>
  241. public static void JavaScriptSetCookie(string strName, string strValue)
  242. {
  243. string js = @"<script language=Javascript>
  244. var the_cookie = '" + strName + "=" + strValue + @"'
  245. var dateexpire = 'Tuesday, 01-Dec-2020 12:00:00 GMT';
  246. //document.cookie = the_cookie;//写入Cookie<BR>} <BR>
  247. document.cookie = the_cookie + '; expires='+dateexpire;
  248. </script>";
  249. HttpContext.Current.Response.Write(js);
  250. }
  251. /// <summary>
  252. /// 函数名:GotoParentWindow
  253. /// 功能描述:返回父窗口
  254. /// 处理流程:
  255. /// 算法描述:
  256. /// 作 者: 孙洪彪
  257. /// 日 期: 2003-04-30 10:00
  258. /// 修 改:
  259. /// 日 期:
  260. /// 版 本:
  261. /// </summary>
  262. /// <param name="parentWindowUrl">父窗口</param>
  263. public static void GotoParentWindow(string parentWindowUrl)
  264. {
  265. string js = @"<Script language='JavaScript'>
  266. this.parent.location.replace('" + parentWindowUrl + "');</Script>";
  267. HttpContext.Current.Response.Write(js);
  268. }
  269. /// <summary>
  270. /// 函数名:ReplaceParentWindow
  271. /// 功能描述:替换父窗口
  272. /// 处理流程:
  273. /// 算法描述:
  274. /// 作 者: 孙洪彪
  275. /// 日 期: 2003-04-30 10:00
  276. /// 修 改:
  277. /// 日 期:
  278. /// 版 本:
  279. /// </summary>
  280. /// <param name="parentWindowUrl">父窗口</param>
  281. /// <param name="caption">窗口提示</param>
  282. /// <param name="future">窗口特征参数</param>
  283. public static void ReplaceParentWindow(string parentWindowUrl, string caption, string future)
  284. {
  285. string js = "";
  286. if (future != null && future.Trim() != "")
  287. {
  288. js = @"<script language=javascript>this.parent.location.replace('" + parentWindowUrl + "','" + caption + "','" + future + "');</script>";
  289. }
  290. else
  291. {
  292. js = @"<script language=javascript>var iWidth = 0 ;var iHeight = 0 ;iWidth=window.screen.availWidth-10;iHeight=window.screen.availHeight-50;
  293. var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';this.parent.location.replace('" + parentWindowUrl + "','" + caption + "',szFeatures);</script>";
  294. }
  295. HttpContext.Current.Response.Write(js);
  296. }
  297. /// <summary>
  298. /// 函数名:ReplaceOpenerWindow
  299. /// 功能描述:替换当前窗体的打开窗口
  300. /// 处理流程:
  301. /// 算法描述:
  302. /// 作 者: 孙洪彪
  303. /// 日 期: 2003-04-30 16:00
  304. /// 修 改:
  305. /// 日 期:
  306. /// 版 本:
  307. /// </summary>
  308. /// <param name="openerWindowUrl">当前窗体的打开窗口</param>
  309. public static void ReplaceOpenerWindow(string openerWindowUrl)
  310. {
  311. string js = @"<Script language='JavaScript'>
  312. window.opener.location.replace('" + openerWindowUrl + "');</Script>";
  313. HttpContext.Current.Response.Write(js);
  314. }
  315. /// <summary>
  316. /// 函数名:ReplaceOpenerParentWindow
  317. /// 功能描述:替换当前窗体的打开窗口的父窗口
  318. /// 处理流程:
  319. /// 算法描述:
  320. /// 作 者: 孙洪彪
  321. /// 日 期: 2003-07-03 19:00
  322. /// 修 改:
  323. /// 日 期:
  324. /// 版 本:
  325. /// </summary>
  326. /// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>
  327. public static void ReplaceOpenerParentFrame(string frameName, string frameWindowUrl)
  328. {
  329. string js = @"<Script language='JavaScript'>
  330. window.opener.parent." + frameName + ".location.replace('" + frameWindowUrl + "');</Script>";
  331. HttpContext.Current.Response.Write(js);
  332. }
  333. /// <summary>
  334. /// 函数名:ReplaceOpenerParentWindow
  335. /// 功能描述:替换当前窗体的打开窗口的父窗口
  336. /// 处理流程:
  337. /// 算法描述:
  338. /// 作 者: 孙洪彪
  339. /// 日 期: 2003-07-03 19:00
  340. /// 修 改:
  341. /// 日 期:
  342. /// 版 本:
  343. /// </summary>
  344. /// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>
  345. public static void ReplaceOpenerParentWindow(string openerParentWindowUrl)
  346. {
  347. string js = @"<Script language='JavaScript'>
  348. window.opener.parent.location.replace('" + openerParentWindowUrl + "');</Script>";
  349. HttpContext.Current.Response.Write(js);
  350. }
  351. /// <summary>
  352. /// 函数名:CloseParentWindow
  353. /// 功能描述:关闭窗口
  354. /// 处理流程:
  355. /// 算法描述:
  356. /// 作 者: 孙洪彪
  357. /// 日 期: 2003-04-30 16:00
  358. /// 修 改:
  359. /// 日 期:
  360. /// 版 本:
  361. /// </summary>
  362. public static void CloseParentWindow()
  363. {
  364. string js = @"<Script language='JavaScript'>
  365. window.parent.close();
  366. </Script>";
  367. HttpContext.Current.Response.Write(js);
  368. }
  369. public static void CloseOpenerWindow()
  370. {
  371. string js = @"<Script language='JavaScript'>
  372. window.opener.close();
  373. </Script>";
  374. HttpContext.Current.Response.Write(js);
  375. }
  376. /// <summary>
  377. /// 函数名:ShowModalDialogJavascript
  378. /// 功能描述:返回打开模式窗口的脚本
  379. /// 处理流程:
  380. /// 算法描述:
  381. /// 作 者: 孙洪彪
  382. /// 日 期: 2003-04-30 15:00
  383. /// 修 改:
  384. /// 日 期:
  385. /// 版 本:
  386. /// </summary>
  387. /// <param name="webFormUrl"></param>
  388. /// <returns></returns>
  389. public static string ShowModalDialogJavascript(string webFormUrl)
  390. {
  391. string js = @"<script language=javascript>
  392. var iWidth = 0 ;
  393. var iHeight = 0 ;
  394. iWidth=window.screen.availWidth-10;
  395. iHeight=window.screen.availHeight-50;
  396. var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';
  397. showModalDialog('" + webFormUrl + "','',szFeatures);</script>";
  398. return js;
  399. }
  400. public static string ShowModalDialogJavascript(string webFormUrl, string features)
  401. {
  402. string js = @"<script language=javascript>
  403. showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
  404. return js;
  405. }
  406. /// <summary>
  407. /// 函数名:ShowModalDialogWindow
  408. /// 功能描述:打开模式窗口
  409. /// 处理流程:
  410. /// 算法描述:
  411. /// 作 者: 孙洪彪
  412. /// 日 期: 2003-04-30 15:00
  413. /// 修 改:
  414. /// 日 期:
  415. /// 版 本:
  416. /// </summary>
  417. /// <param name="webFormUrl"></param>
  418. /// <returns></returns>
  419. public static void ShowModalDialogWindow(string webFormUrl)
  420. {
  421. string js = ShowModalDialogJavascript(webFormUrl);
  422. HttpContext.Current.Response.Write(js);
  423. }
  424. public static void ShowModalDialogWindow(string webFormUrl, string features)
  425. {
  426. string js = ShowModalDialogJavascript(webFormUrl, features);
  427. HttpContext.Current.Response.Write(js);
  428. }
  429. public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
  430. {
  431. string features = "dialogWidth:" + width.ToString() + "px"
  432. + ";dialogHeight:" + height.ToString() + "px"
  433. + ";dialogLeft:" + left.ToString() + "px"
  434. + ";dialogTop:" + top.ToString() + "px"
  435. + ";center:yes;help=no;resizable:no;status:no;scroll=no";
  436. ShowModalDialogWindow(webFormUrl, features);
  437. }
  438. public static void SetHtmlElementValue(string formName, string elementName, string elementValue)
  439. {
  440. string js = @"<Script language='JavaScript'>if(document." + formName + "." + elementName + "!=null){document." + formName + "." + elementName + ".value =" + elementValue + ";}</Script>";
  441. HttpContext.Current.Response.Write(js);
  442. }
  443. }
  444. }