JScript.cs
上传用户:autodoor
上传日期:2022-08-04
资源大小:9973k
文件大小:15k
源码类别:

.net编程

开发平台:

Others

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