asp.net在伺服器端運行,是不能在伺服器端彈出對話方塊的,但是C#可以通過在頁面輸出JS代碼實現彈出訊息框的效果,這個C#類封裝了常用的訊息框彈出JS代碼,可以在伺服器端調用,在用戶端顯示對話方塊。不但可以顯示JS的警告框,還可以顯示模式視窗,非常方便。
using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace DotNet.Utilities{ /// /// 頁面常用方法封裝 /// public class ShowMessageBox { #region 資訊顯示 /// /// 顯示提示資訊 /// /// public static void ShowMG(string message) { WriteScript("alert('" + message + "');"); } /// /// 顯示提示資訊 /// /// 提示資訊 public static void ShowMessage(string message) { ShowMessage("系統提示", 180, 120, message); } /// /// 顯示提示資訊 /// /// 提示資訊 public static void ShowMessage_link(string message, string linkurl) { ShowMessage_link("系統提示", 180, 120, message, linkurl, 8000, -1); } /// /// 顯示提示資訊 /// /// /// /// /// 提示資訊 private static void ShowMessage(string title, int width, int height, string message) { ShowMessage(title, width, height, message, 3000, -1); } /// /// 顯示提示資訊 /// /// /// /// /// /// /// private static void ShowMessage(string title, int width, int height, string message, int delayms, int leftSpace) { WriteScript(string.Format("popMessage({0},{1},'{2}','{3}',{4},{5});", width, height, title, message, delayms, leftSpace == -1 ? "null" : leftSpace.ToString())); } /// /// 顯示提示資訊 /// /// /// /// /// /// /// private static void ShowMessage_link(string title, int width, int height, string message, string linkurl, int delayms, int leftSpace) { WriteScript(string.Format("popMessage2({0},{1},'{2}','{3}','{4}',{5},{6});", width, height, title, message, linkurl, delayms, leftSpace == -1 ? "null" : leftSpace.ToString())); } #endregion #region 顯示異常資訊 /// /// 顯示異常資訊 /// /// public static void ShowExceptionMessage(Exception ex) { ShowExceptionMessage(ex.Message); } /// /// 顯示異常資訊 /// /// public static void ShowExceptionMessage(string message) { WriteScript("alert('" + message + "');"); //PageHelper.ShowExceptionMessage("錯誤提示", 210, 125, message); } /// /// 顯示異常資訊 /// /// /// /// /// private static void ShowExceptionMessage(string title, int width, int height, string message) { WriteScript(string.Format("setTimeout(\"showAlert('{0}',{1},{2},'{3}')\",100);", title, width, height, message)); } #endregion #region 顯示模態視窗 /// /// 返回把指定連結地址顯示模態視窗的指令碼 /// /// /// /// /// /// public static string GetShowModalWindowScript(string wid, string title, int width, int height, string url) { return string.Format("setTimeout(\"showModalWindow('{0}','{1}',{2},{3},'{4}')\",100);", wid, title, width, height, url); } /// /// 把指定連結地址顯示模態視窗 /// /// 視窗ID /// 標題 /// 寬度 /// 高度 /// 連結地址 public static void ShowModalWindow(string wid, string title, int width, int height, string url) { WriteScript(GetShowModalWindowScript(wid, title, width, height, url)); } /// /// 為指定控制項綁定前台指令碼:顯示模態視窗 /// /// /// /// /// /// /// /// /// public static void ShowCilentModalWindow(string wid, WebControl control, string eventName, string title, int width, int height, string url, bool isScriptEnd) { string script = isScriptEnd ? "return false;" : ""; control.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url); } /// /// 為指定控制項綁定前台指令碼:顯示模態視窗 /// /// /// /// /// /// /// /// /// public static void ShowCilentModalWindow(string wid, TableCell cell, string eventName, string title, int width, int height, string url, bool isScriptEnd) { string script = isScriptEnd ? "return false;" : ""; cell.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url); } #endregion #region 顯示用戶端確認視窗 /// /// 顯示用戶端確認視窗 /// /// /// /// public static void ShowCilentConfirm(WebControl control, string eventName, string message) { ShowCilentConfirm(control, eventName, "系統提示", 210, 125, message); } /// /// 顯示用戶端確認視窗 /// /// /// /// /// /// /// public static void ShowCilentConfirm(WebControl control, string eventName, string title, int width, int height, string message) { control.Attributes[eventName] = string.Format("return showConfirm('{0}',{1},{2},'{3}','{4}');", title, width, height, message, control.ClientID); } #endregion /// /// 寫javascript指令碼 /// /// 指令碼內容 public static void WriteScript(string script) { Page page = GetCurrentPage(); // NDGridViewScriptFirst(page.Form.Controls, page); page.ClientScript.RegisterStartupScript(page.GetType(), System.Guid.NewGuid().ToString(), script, true); } /// /// 得到當前頁對象執行個體 /// /// public static Page GetCurrentPage() { return (Page)HttpContext.Current.Handler; } }}