Asp.net消息提示封装类,类似winform中的MessageBox
using System; using System.Data; using System.Text; using System.Web.UI; public static class MessageBox { const string javaScriptHead = "<script type='text/javascript'>"; const string javaScriptFoot = "</script>"; /// <summary> /// Page.ClientScript.RegisterStartupScript提示消息 /// 注意:模版页中提示消息不能用此方法;消息中有换行用\\r\\n /// </summary> /// <remarks> /// MessageBox.Show("提示信息!",this); /// </remarks> /// <param name="message">提示信息</param> /// <param name="page">页面对象,一般传入this</param> public static void Show(string message, Page page) { Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, javaScriptHead + "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "')" + javaScriptFoot); } /// <summary> /// ScriptManager.RegisterStartupScript提示消息 /// </summary> /// <param name="message">提示信息</param> /// <param name="updatePanel">UpdatePanel对象</param> public static void Show(string message, UpdatePanel updatePanel) { ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), updatePanel.ID, "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');", true); } /// <summary> /// 通过RegisterClientScriptBlock提示消息 /// </summary> /// <param name="message">提示信息</param> /// <param name="page">页面对象,一般传入this</param> public static void ShowByBlock(string message, Page page) { Type pageType = page.GetType(); page.ClientScript.RegisterClientScriptBlock(pageType, pageType.Name, "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "')", true); } /// <summary> /// confirm确认提示消息后,跳转到指定页面,Page.ClientScript.RegisterStartupScript提示消息 /// </summary> /// <remarks> /// MessageBox.ShowConfirmAndRedirect("提示消息!","Success.aspx",this); /// </remarks> /// <param name="message">提示消息</param> /// <param name="url">跳转的页面</param> /// <param name="page">页面对象,一般传入this</param> public static void ShowConfirmAndRedirect(string message, string url, Page page) { string script = "var ok=false;"; if (!string.IsNullOrEmpty(message)) script = "ok=confirm('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; if (!string.IsNullOrEmpty(url)) script += "if(ok==true)top.location.href='" + url + "'"; script = javaScriptHead + script + javaScriptFoot; Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, script); } /// <summary> /// 提示消息后刷新本页面,Page.ClientScript.RegisterStartupScript提示消息 /// </summary> /// <param name="message">提示消息</param> /// <param name="parentFrameId">父frame的id</param> /// <param name="page">页面对象,一般传入this</param> public static void ShowAndRefreshParentFrame(string message, string parentFrameId, Page page) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; //script += "window.location.href=window.location.href+(window.location.href.indexOf('?')>=0?'&':'?')+Math.random();"; if (!string.IsNullOrEmpty(parentFrameId)) script += "var parentFrame=window.parent.document.getElementById('" + parentFrameId + "');var path=parentFrame.getAttribute('src');path=(path.indexOf('?')>=0?'&':'?')+Math.random();parentFrame.setAttribute('src', path);"; script = javaScriptHead + script + javaScriptFoot; Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, script); } /// <summary> /// 提示消息后刷新指定父页面,Page.ClientScript.RegisterStartupScript提示消息 /// </summary> /// <param name="message">提示消息</param> /// <param name="url">父窗口要跳转的页面</param> /// <param name="page">页面对象,一般传入this</param> public static void ShowAndRefreshParentAndClose(string message, string url, Page page) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; if (!string.IsNullOrEmpty(url)) script += "window.opener.location.href='" + url + "';window.close();"; script = javaScriptHead + script + javaScriptFoot; Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, script); } /// <summary> /// window.open打开窗口 /// </summary> /// <param name="url">要打开的页面</param> /// <param name="width">窗口宽度</param> /// <param name="height">窗口高度</param> /// <param name="page">页面对象,一般传入this</param> public static void OpenWindow(string url, int width, int height, Page page) { string script = javaScriptHead + "var newwindow=window.open('" + url + "','_blank','width=" + width + ",height=" + height + ",top='+(screen.height-" + height + ")/2+',left='+(screen.width-" + width + ")/2+',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');newwindow.focus();" + javaScriptFoot; Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, script); } /// <summary> /// 弹出居中模态窗口 /// </summary> /// <param name="url">页面地址</param> /// <param name="width">模态窗口宽度</param> /// <param name="height">模态窗口高度</param> /// <param name="page">页面对象,一般传入this</param> public static void ShowModalDialog(string url, int width, int height, Page page) { string script = javaScriptHead + "window.showModalDialog('" + url + "','','dialogWidth:" + width + "px;dialogHeight:" + height + "px;dialogTop:'+(screen.height-" + height + ")/2+'px;dialogLeft:'+(screen.width-" + width + ")/2+'px;center:1;scroll:no;status:no;');" + javaScriptFoot; Type pageType = page.GetType(); page.ClientScript.RegisterStartupScript(pageType, pageType.Name, script); } #region Response.Write 提示消息 /// <summary> /// Response提示消息后跳转到指定页面 /// window.location.href /// </summary> /// <param name="message">提示信息</param> /// <param name="url">跳转的页面</param> public static void ResponseMsgAndRedirect(string message, string url) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; if (!string.IsNullOrEmpty(url)) script += "window.location.href='" + url + "'"; script = javaScriptHead + script + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } /// <summary> /// Response提示消息后,子页面触发父页面跳转,适用于iframe /// parent.location.href /// </summary> /// <param name="message">提示信息</param> /// <param name="url">要跳转页面</param> public static void ResponseMsgAndParentRedirect(string message, string url) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; if (!string.IsNullOrEmpty(url)) script += "parent.location.href='" + url + "'"; script = javaScriptHead + script + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } /// <summary> /// Response提示消息后,最顶层的页面跳转,适用于iframe中子页面触发顶层页面跳转 /// top.location.href /// </summary> /// <param name="message">提示信息</param> /// <param name="url">要跳转的页面</param> public static void ResponseMsgAndTopRedirect(string message, string url) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; if (!string.IsNullOrEmpty(url)) script += "top.location.href='" + url + "'"; script = javaScriptHead + script + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } /// <summary> /// Response提示消息后返回指定页面 /// </summary> /// <param name="message">提示信息</param> /// <param name="historyCount">history.go(historyCount)</param> public static void ResponseMsgAndHistoryBack(string message, int historyCount) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; script = javaScriptHead + script + "history.go(" + historyCount + ")" + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } /// <summary> /// 提示消息后并关闭页面 /// </summary> /// <param name="message">提示信息</param> public static void ResponseMsgAndClose(string message) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; script = javaScriptHead + script + "window.opener=null;window.open('', '_self', '');window.close()" + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } /// <summary> /// 提示消息后并关闭页面,并刷新父页面 /// </summary> /// <param name="message">提示信息</param> /// <param name="url">父页面跳转的页面</param> public static void ResponseMsgAndCloseAndParentRedirect(string message, string url) { string script = string.Empty; if (!string.IsNullOrEmpty(message)) script = "alert('" + message.Replace("'", string.Empty).Replace("\\r", string.Empty).Replace("\\n", string.Empty) + "');"; script = javaScriptHead + script + ";window.open('', '_self', '');window.close();window.opener.location ='" + url + "';" + javaScriptFoot; System.Web.HttpContext.Current.Response.Write(script); } #endregion }