Asp.net中向前端输出JS的一些调用

最近突然写ASP.NET项目,用到向前台输出JS脚本,但是以前在MVC里是通过异步或者一些方法来调用,但是ASP.net用到的很少。在网上找到一个HELPER.CS.保存一下,以后再用。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web;
 6 using System.Web.UI;
 7 namespace CLB.Utility.WebTools
 8 {
 9     public static class JsHelper
10     {
11         public static void SetJS(string js, Page page, string jsName)
12         {
13             string str = "<script type="text/javascript" language="javascript">{0}</script>";
14             js = string.Format(str, js);
15             if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), jsName))
16             {
17                 page.ClientScript.RegisterStartupScript(page.GetType(), jsName, js);
18             }
19         }
20         ///<summary>
21         ///弹出JavaScript小窗口
22         ///</summary>
23         ///<paramname="js">窗口信息</param>
24         public static void Alert(string message, Page page)
25         {
26             string js = @"alert('" + message + "');";
27             SetJS(js, page, "alert");
28         }
29         ///<summary>
30         ///弹出消息框并且转向到新的URL
31         ///</summary>
32         ///<paramname="message">消息内容</param>
33         ///<paramname="toURL">连接地址</param>
34         public static void AlertAndRedirect(string message, string toURL, Page page)
35         {
36             #region
37             string js = string.Format("alert('{0}');window.location.replace('{1}')", message, toURL);
38             SetJS(js, page, "AlertAndRedirect");
39             #endregion
40         }
41         ///<summary>
42         ///回到历史页面
43         ///</summary>
44         ///<paramname="value">-1/1</param>
45         public static void GoHistory(int value, Page page)
46         {
47             string js = string.Format("history.go({0});", value);
48             SetJS(js, page, "GoHistory");
49         }
50         ///<summary>
51         ///刷新父窗口
52         ///</summary>
53         public static void RefreshParent(string url, Page page)
54         {
55             #region
56             string js = @"window.opener.location.href='" + url + "';window.close();";
57             SetJS(js, page, "RefreshParent");
58             #endregion
59         }
60         /// <summary>
61         /// 刷新打开窗口
62         /// </summary>
63         /// <param name="page"></param>
64         public static void RefreshOpener(Page page)
65         {
66             #region
67             string js = @"opener.location.reload();";
68             SetJS(js, page, "RefreshOpener");
69             #endregion
70         }///<summary>
71         ///打开指定大小位置的模式对话框
72         ///</summary>
73         ///<paramname="webFormUrl">连接地址</param>
74         ///<paramname="width"></param>
75         ///<paramname="height"></param>
76         ///<paramname="top">距离上位置</param>
77         ///<paramname="left">距离左位置</param>
78         public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left, Page page)
79         {
80             #region
81             string features = "dialogWidth:" + width.ToString() + "px"
82               + ";dialogHeight:" + height.ToString() + "px"
83               + ";dialogLeft:" + left.ToString() + "px"
84               + ";dialogTop:" + top.ToString() + "px"
85               + ";center:yes;help=no;resizable:no;status:no;scroll=yes";
86             ShowModalDialogWindow(webFormUrl, features, page);
87             #endregion
88         }
89         public static void ShowModalDialogWindow(string webFormUrl, string features, Page page)
90         {
91             string js = @"showModalDialog('" + webFormUrl + "','','" + features + "');";
92             SetJS(js, page, "ShowModalDialogWindow");
93         }
94     }
95 }
View Code

转自http://www.codefans.net/articles/1680.shtml 谢谢作者分享

原文地址:https://www.cnblogs.com/qzzy/p/4555688.html