ASP.NET伺服器端執行指令碼的通用方法
1、彈出JavaScript小視窗
public static void Alert(string message) { string js = "<script language='JavaScript'>alert('{0}');history.go(-1);</script>"; HttpContext.Current.Response.Write(string.Format(js, message)); }
2、彈出訊息框並且轉向到新的URL
public static void Alert(string message, string toURL) { string js = "<script language='JavaScript'>alert('{0}');window.location.replace('{1}')</script>"; HttpContext.Current.Response.Write(string.Format(js, message, toURL)); }
3、出訊息框並且轉向到指定FRAME的新URL public static void Alert(string message, string frame, string toURL) { string js = "<script language='JavaScript'>alert('{0}');{1}.location.replace('{2}')</script>"; HttpContext.Current.Response.Write(string.Format(js, message, frame, toURL)); }
4、伺服器端彈出confirm對話方塊,並根據選擇觸發某按鈕點選事重定向URL。 public static void ConfirmRedirect(Page page, string sMessage, string sUrl) { page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){ window.location.href = '" + sUrl + "';}</script>"); }
5、伺服器端彈出confirm對話方塊,並根據選擇觸發某按鈕點選事件。 public static void Confirm(Page page, string sMessage, string btn) { page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){document.forms(0)." + btn + ".click();}</script>"); }
6、伺服器端彈出confirm對話方塊,並根據選擇觸發某按鈕點選事重定向URL,包括“確定”和“取消”時的操作 public static void Confirm(Page page, string sMessage, string btn_Redirect_Yes, string btn_Redirect_No) { page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){document.forms(0)." + btn_Redirect_Yes + ".click();}btn_Redirect_Yes{document.forms(0)." + btn_Redirect_No + ".click();}</script>"); }
7、回到歷史頁面 public static void GoHistory(int value) { string js = "<script language='JavaScript'>history.go({0});</script>"; HttpContext.Current.Response.Write(string.Format(js, value)); }
8、關閉當前視窗 public static void CloseWindow() { string js = "<script language='JavaScript'>parent.opener=null;window.close();</script>"; HttpContext.Current.Response.Write(js); HttpContext.Current.Response.End(); }
9、父視窗開啟指定頁面 public static void RefreshParent(string url) { string js = "<script language='JavaScript'>window.opener.location.href='{0}';window.close();</script>"; HttpContext.Current.Response.Write(string.Format(js, url)); }
10、重新整理視窗 public static void RefreshWindow() { string js = "<script language='JavaScript'>opener.location.reload();</script>"; HttpContext.Current.Response.Write(js); }
11、 開啟指定大小的新窗體 public static void OpenWindow(string url, int width, int heigth, int top, int left) { string js = "<script language='JavaScript'>window.open('{0}', '', 'width={1}, height={2}, top={3}, left={4}, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no');</script>"; HttpContext.Current.Response.Write(string.Format(js, url, width, heigth, top, left)); }
12、開啟新窗體 public static void OpenWindow(string url) { string js = "<script language='JavaScript'>window.open('{0}');</script>"; HttpContext.Current.Response.Write(string.Format(js, url)); }
13、 轉向指定視窗指定Url的頁面 public static void JsGoTo(string window, string url) { string js = "<script language='JavaScript'>{0}.location.replace('{1}');</script>"; HttpContext.Current.Response.Write(string.Format(js, window, url)); }
14、轉向指定Url的頁面 public static void JavaScriptLocationHref(string url) { JsGoTo("window", url); }
15、轉向top指定Url的頁面 public static void TopGoTo(string url) { JsGoTo("top", url); }
16、轉向parent指定Url的頁面 public static void ParentGoTo(string url) { JsGoTo("parent", url); }
17、開啟指定大小位置的模式對話方塊 public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left) { string features = "dialogWidth:" + width.ToString() + "px" + ";dialogHeight:" + height.ToString() + "px" + ";dialogLeft:" + left.ToString() + "px" + ";dialogTop:" + top.ToString() + "px" + ";center:yes;help=no;resizable:no;status:no;scroll=yes"; ShowModalDialogWindow(webFormUrl, features); }
18、開啟模式視窗 public static void ShowModalDialogWindow(string webFormUrl, string features) { string js = ShowModalDialogJavascript(webFormUrl, features); HttpContext.Current.Response.Write(js); }
19、返回模式視窗指令碼 public static string ShowModalDialogJavascript(string webFormUrl, string features) { string js = "<script language=javascript>showModalDialog('" + webFormUrl + "','','" + features + "');</script>"; return js; }