public static void ShowMessageBox(string msg) { // Get a ClientScriptManager reference from the Page class. msg = msg.Replace("'", " "); msg = msg.Replace("\r", ""); msg = msg.Replace("\n", ""); if (HttpContext.Current == null) { throw new Exception("Method must be called from a page context"); } Page page = HttpContext.Current.Handler as Page; if (page == null) { throw new Exception("Method must be called from a page context"); } ClientScriptManager cs = page.ClientScript; // Check to see if the startup script is already registered. if (!cs.IsStartupScriptRegistered(cs.GetType(), "PopupScript")) { //cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('" + msg + "');", true); cs.RegisterStartupScript(cs.GetType(), "PopupScript", "alert('" + msg + "');", true); } }
/// <summary> /// 以弹窗形式向客户端发送提示信息 /// </summary> /// <param name="csm">页面的Page.ClientScript属性</param> /// <param name="Msg">提示信息</param> public static void PrintMsgToClient(ClientScriptManager csm, string Msg) { if (!csm.IsClientScriptBlockRegistered(csm.GetType(), "Tip"))//检验是否已注册客户端脚本 { //输出提示信息 csm.RegisterStartupScript(csm.GetType(), "Tip", "<script>alert('" + Msg + "');</script>", false); } }
/// <summary> /// 显示客户端是与否的对话框 /// </summary> /// <param name="scriptKey">脚本的键-用于避免重复注册客户端脚本</param> /// <param name="alertText">提示文本</param> public static void ShowConfirm(Page page, string scriptKey, string alertText, string doText) { alertText = alertText.Replace("\"", "\\\""); ClientScriptManager cs = page.ClientScript; if (!cs.IsStartupScriptRegistered(cs.GetType(), scriptKey)) { cs.RegisterStartupScript(cs.GetType(), scriptKey, string.Format("<script>if(window.confirm('{0}')){1}</script>", alertText, doText)); } }
/// <summary> /// 显示客户端提示对话框信息 /// </summary> /// <param name="scriptKey">脚本的键-用于避免重复注册客户端脚本</param> /// <param name="alertText">提示文本</param> public static void ShowClientAlert(Page page, string scriptKey, string alertText) { alertText = alertText.Replace("\"", "\\\""); ClientScriptManager cs = page.ClientScript; if (!cs.IsStartupScriptRegistered(cs.GetType(), scriptKey)) { cs.RegisterStartupScript(cs.GetType(), scriptKey, string.Format("<script language=\"javascript\" type=\"text/javascript\">alert(\"{0}\");</script>\r\n", alertText)); } }
public static void ShowControl(this System.Web.UI.Page page, string dialogID, string titleName) { ClientScriptManager csm = page.ClientScript; string Script = @" <script type=text/javascript> var beforeAction = window.onload || function () {{}}; window.onload = function () {{ beforeAction(); LoadControl('{0}', '{1}', '{2}'); }} </script>"; // 注意,有經過String.Format的話大括號要重複 csm.RegisterStartupScript(csm.GetType(), "Show", String.Format(Script, dialogID, titleName, page.ResolveUrl("~/img/closebutton.ico"))); }