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 ///21 ///弹出JavaScript小窗口 22 /// 23 /// 窗口信息 24 public static void Alert(string message, Page page) 25 { 26 string js = @"alert('" + message + "');"; 27 SetJS(js, page, "alert"); 28 } 29 /// 30 ///弹出消息框并且转向到新的URL 31 /// 32 /// 消息内容 33 /// 连接地址 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 /// 42 ///回到历史页面 43 /// 44 /// -1/1 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 /// 51 ///刷新父窗口 52 /// 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 /// 61 /// 刷新打开窗口 62 /// 63 /// 64 public static void RefreshOpener(Page page) 65 { 66 #region 67 string js = @"opener.location.reload();"; 68 SetJS(js, page, "RefreshOpener"); 69 #endregion 70 }/// 71 ///打开指定大小位置的模式对话框 72 /// 73 /// 连接地址 74 /// 宽 75 /// 高 76 /// 距离上位置 77 /// 距离左位置 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 }
转自http://www.codefans.net/articles/1680.shtml 谢谢作者分享