1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.UI;
  6. /// <summary>
  7. /// 一些常用的Js调用
  8. /// 采用ClientScript.RegisterStartupScript(string msg)的方式输出,不会改变xhtml的结构,
  9. /// 不会影响执行效果。
  10. /// 为了向下兼容,采用了重载的方式,新版本中要求一个System.Web.UI.Page类的实例。
  11. /// </summary>
  12. public class JScript
  13. {
  14. #region 实现方法
  15. /// <summary>
  16. /// 弹出JavaScript小窗口
  17. /// </summary>
  18. /// <param name="js">窗口信息</param>
  19. public static void Alert(string message, Page page)
  20. {
  21. #region
  22. string js = @"<Script language='JavaScript'>
  23. alert('" + message + "');</Script>";
  24. //HttpContext.Current.Response.Write(js);
  25. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
  26. {
  27. page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
  28. }
  29. #endregion
  30. }
  31. /// <summary>
  32. /// 弹出消息框并且转向到新的URL
  33. /// </summary>
  34. /// <param name="message">消息内容</param>
  35. /// <param name="toURL">连接地址</param>
  36. public static void AlertAndRedirect(string message, string toURL, Page page)
  37. {
  38. #region
  39. string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
  40. //HttpContext.Current.Response.Write(string.Format(js, message, toURL));
  41. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
  42. {
  43. page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", string.Format(js, message, toURL));
  44. }
  45. #endregion
  46. }
  47. /// <summary>
  48. /// 回到历史页面
  49. /// </summary>
  50. /// <param name="value">-1/1</param>
  51. public static void GoHistory(int value, Page page)
  52. {
  53. #region
  54. string js = @"<Script language='JavaScript'>
  55. history.go({0});
  56. </Script>";
  57. //HttpContext.Current.Response.Write(string.Format(js, value));
  58. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "GoHistory"))
  59. {
  60. page.ClientScript.RegisterStartupScript(page.GetType(), "GoHistory", string.Format(js, value));
  61. }
  62. #endregion
  63. }
  64. //        /// <summary>
  65. //        /// 关闭当前窗口
  66. //        /// </summary>
  67. //        public static void CloseWindow()
  68. //        {
  69. //            #region
  70. //            string js = @"<Script language='JavaScript'>
  71. //                    parent.opener=null;window.close();
  72. //                  </Script>";
  73. //            HttpContext.Current.Response.Write(js);
  74. //            HttpContext.Current.Response.End();
  75. //            #endregion
  76. //        }
  77. /// <summary>
  78. /// 刷新父窗口
  79. /// </summary>
  80. public static void RefreshParent(string url, Page page)
  81. {
  82. #region
  83. string js = @"<Script language='JavaScript'>
  84. window.opener.location.href='" + url + "';window.close();</Script>";
  85. //HttpContext.Current.Response.Write(js);
  86. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent"))
  87. {
  88. page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", js);
  89. }
  90. #endregion
  91. }
  92. /// <summary>
  93. /// 刷新打开窗口
  94. /// </summary>
  95. public static void RefreshOpener(Page page)
  96. {
  97. #region
  98. string js = @"<Script language='JavaScript'>
  99. opener.location.reload();
  100. </Script>";
  101. //HttpContext.Current.Response.Write(js);
  102. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshOpener"))
  103. {
  104. page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshOpener", js);
  105. }
  106. #endregion
  107. }
  108. /// <summary>
  109. /// 打开指定大小的新窗体
  110. /// </summary>
  111. /// <param name="url">地址</param>
  112. /// <param name="width">宽</param>
  113. /// <param name="heigth">高</param>
  114. /// <param name="top">头位置</param>
  115. /// <param name="left">左位置</param>
  116. public static void OpenWebFormSize(string url, int width, int heigth, int top, int left, Page page)
  117. {
  118. #region
  119. 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>";
  120. //HttpContext.Current.Response.Write(js);
  121. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFormSize"))
  122. {
  123. page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFormSize", js);
  124. }
  125. #endregion
  126. }
  127. /// <summary>
  128. /// 转向Url制定的页面
  129. /// </summary>
  130. /// <param name="url">连接地址</param>
  131. public static void JavaScriptLocationHref(string url, Page page)
  132. {
  133. #region
  134. string js = @"<Script language='JavaScript'>
  135. window.location.replace('{0}');
  136. </Script>";
  137. js = string.Format(js, url);
  138. //HttpContext.Current.Response.Write(js);
  139. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "JavaScriptLocationHref"))
  140. {
  141. page.ClientScript.RegisterStartupScript(page.GetType(), "JavaScriptLocationHref", js);
  142. }
  143. #endregion
  144. }
  145. /// <summary>
  146. /// 打开指定大小位置的模式对话框
  147. /// </summary>
  148. /// <param name="webFormUrl">连接地址</param>
  149. /// <param name="width">宽</param>
  150. /// <param name="height">高</param>
  151. /// <param name="top">距离上位置</param>
  152. /// <param name="left">距离左位置</param>
  153. public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left, Page page)
  154. {
  155. #region
  156. string features = "dialogWidth:" + width.ToString() + "px"
  157. + ";dialogHeight:" + height.ToString() + "px"
  158. + ";dialogLeft:" + left.ToString() + "px"
  159. + ";dialogTop:" + top.ToString() + "px"
  160. + ";center:yes;help=no;resizable:no;status:no;scroll=yes";
  161. ShowModalDialogWindow(webFormUrl, features, page);
  162. #endregion
  163. }
  164. /// <summary>
  165. /// 弹出模态窗口
  166. /// </summary>
  167. /// <param name="webFormUrl"></param>
  168. /// <param name="features"></param>
  169. public static void ShowModalDialogWindow(string webFormUrl, string features, Page page)
  170. {
  171. string js = ShowModalDialogJavascript(webFormUrl, features);
  172. //HttpContext.Current.Response.Write(js);
  173. if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "ShowModalDialogWindow"))
  174. {
  175. page.ClientScript.RegisterStartupScript(page.GetType(), "ShowModalDialogWindow", js);
  176. }
  177. }
  178. private static string ShowModalDialogJavascript(string webFormUrl, string features)
  179. {
  180. throw new NotImplementedException();
  181. }
  182. //        /// <summary>
  183. //        /// 弹出模态窗口
  184. //        /// </summary>
  185. //        /// <param name="webFormUrl"></param>
  186. //        /// <param name="features"></param>
  187. //        /// <returns></returns>
  188. //        public static string ShowModalDialogJavascript(string webFormUrl, string features)
  189. //        {
  190. //            #region
  191. //            string js = @"<script language=javascript>
  192. //    showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
  193. //            return js;
  194. //            #endregion
  195. //        }
  196. #endregion
  197. }

ASP.NET中常用输出JS脚本的类(来自于周公博客)的更多相关文章

  1. ASP.NET中后台注册js脚本攻略(转)

    用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:区别:   1.使用Page ...

  2. ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl

    ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库     源码下载Maticsoft.DBUtility.dll 数据访问类库组件     源码下载LtpPageC ...

  3. ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl (转)

    ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库     源码下载Maticsoft.DBUtility.dll 数据访问类库组件     源码下载LtpPageC ...

  4. C#中如何使用JS脚本

    C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...

  5. 请解释ASP. NET中的web页面与隐藏类之间的关系

    请解释ASP.NET中的web页面与其隐藏类之间的关系 其实页面与其隐藏类之间就是一个部分类的关系,你在页面上放一个一个的控件就是在这个类中定义一个一个的属性, 因为是同一个类的部分类的关系,所以隐藏 ...

  6. 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站

    这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...

  7. python中逐行读取文件的最佳方式_Drupal_新浪博客

    python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式    (2010-08-18 15:59:28)    转载▼    标签:    python   ...

  8. LIGHTX-CMS —— 基于 Node.js,Express.js 以及 SQLite 3 搭建的个人博客系统

    概述 LIGHTX-CMS 是我基于 Node.js,Express.js 以及 SQLite 3 搭建的个人博客发布系统. 项目本身可以拿来部署个人博客网站,同时我认为其也适合用以新手学习 Node ...

  9. C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客

    原文:C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客 public static List<T> CreateTarInterface& ...

随机推荐

  1. 解决亚马逊云服务器上安装nginx后无法访问的问题

    在亚马逊云服务器上装了Ubuntu系统,使用docker环境搭建nginx,启动nginx容器后,在浏览器输入地址后,显示连接超时. 在网上查了一下说有可能是服务器安全组的设置问题 然后在云服务器的安 ...

  2. 6th Alpha阶段的postmortem报告

    组名:好好学习(代组长发布)  会议重要内容记录: 1.  尝试在beta阶段实现的功能,与alpha阶段相比的优势 (1)更改软件现有的bug: 1)软件的账目只能输入,但是一旦发生失误却无法更改和 ...

  3. MYSQLD_OPTS修改

    systemctl set-environment MYSQLD_OPTS="--skip-grant-tables";

  4. [转帖]USB-C和Thunderbolt 3连接线你搞懂了吗?---没搞明白.

    USB-C和Thunderbolt 3连接线你搞懂了吗? 2018年11月25日 07:30 6318 次阅读 稿源:威锋网 3 条评论 按照计算行业的风潮,USB Type-C 将会是下一代主流的接 ...

  5. 初入码田--ASP.NET MVC4 Web应用开发之二 实现简单的增删改查

    初入码田--ASP.NET MVC4 Web应用之创建一个空白的MVC应用程序 初入码田--ASP.NET MVC4 Web应用开发之一 实现简单的登录 2016-07-29  一.创建M002Adm ...

  6. rpc 协议规范 之 rmi http webservice 和 一些框架

    RPC(Remote Procedure Call)是远程调用,是一种思想,也是一种协议规范.简单地说就是能使应用像调用本地方法一样的调用远程的过程或服务,可以应用在分布式服务.分布式计算.远程服务调 ...

  7. 用join取代not in

    写了好几个页面,速度都上不去,瓶颈在于SQL查询.太多的表,太多的not in,总是从一大推表和数据中筛选出一点数据.看了很多关于SQL优化的文章,都强烈要求不要太多使用not in查询,最好用表连接 ...

  8. BZOJ5120 无限之环(费用流)

    方案合法相当于要求接口之间配对,黑白染色一波,考虑网络流.有一个很奇怪的限制是不能旋转直线型水管,考虑非直线型水管有什么特殊性,可以发现其接口都是连续的.那么对于旋转水管,可以看做是把顺/逆时针方向上 ...

  9. MySQL 双主问题集

    最近试用MySQL高可用方案,需要配MySQL双主,对期间遇到的问题做下记录. 1.导出锁表问题 mysqldump 命令增加参数 --skip-opt -q 可避免导出时锁表: 2.导出\导入所有数 ...

  10. Bob Waters - Twenty Years

    We were just children and grown up closeHow we made it this far only god knowsWe bend the rulesSmash ...