分享一下在aspx页面弹框的设置代码

public static class MessageBox
{
/// <summary>
/// 显示消息提示对话框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void Show(System.Web.UI.Page page, string msg)
{
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "message", "<script type=\"text/javascript\" language=\"javascript\" defer>alert('" + StrSafe(msg) + "');</script>");
}

/// <summary>
/// 显示消息提示对话框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void Show(System.Web.UI.Page page, string key, string msg)
{
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), key, "<script type=\"text/javascript\" language=\"javascript\" defer>alert('" + StrSafe(msg) + "');</script>");
}

/// <summary>
/// 提示信息关闭页面,信息可为空不显示。
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息,为空则不提示</param>
public static void Close(System.Web.UI.Page page, string msg)
{
string js = "<script type=\"text/javascript\" language=\"javascript\" defer>";
if (msg != "")
js += "alert('" + msg.ToString() + "');";
js += "window.close();</script>";
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "message", js);
}
/// <summary>
/// 提示信息关闭页面,信息可为空不显示。
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息,为空则不提示</param>
public static void Close(System.Web.UI.Page page, string key, string msg)
{
string js = "<script type=\"text/javascript\" language=\"javascript\" defer>";
if (msg != "")
js += "alert('" + msg.ToString() + "');";
js += "window.close();</script>";
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), key, js);
}

/// <summary>
/// 屏蔽提示关闭窗口
/// </summary>
/// <param name="page"></param>
/// <param name="msg"></param>
public static void DClose(System.Web.UI.Page page, string msg)
{
StringBuilder js = new StringBuilder("<script type=\"text/javascript\" language=\"javascript\" defer>");
if (msg != "")
js.AppendLine("alert('" + msg.ToString() + "');");
js.AppendLine("var ua = navigator.userAgent; var ie = navigator.appName==\"Microsoft Internet Explorer\"?true:false;");
js.AppendLine("if(ie){");
js.AppendLine("var IEversion = parseFloat(ua.substring(ua.indexOf(\"MSIE \")+5,ua.indexOf(\";\",ua.indexOf(\"MSIE \"))));");
js.AppendLine("if( IEversion< 5.5){");
js.AppendLine("var str = '';");
js.AppendLine("document.body.insertAdjacentHTML(\"beforeEnd\", str);");
js.AppendLine("document.all.noTipClose.Click();");
js.AppendLine("} else {");
js.AppendLine("window.opener =null; window.close();");
js.AppendLine("}");
js.AppendLine("}else{");
js.AppendLine("window.close();");
js.AppendLine("</script>");
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "message", js.ToString());
}

/// <summary>
/// 屏蔽提示关闭窗口
/// </summary>
/// <param name="page"></param>
/// <param name="msg"></param>
public static void DClose(System.Web.UI.Page page, string key, string msg)
{
StringBuilder js = new StringBuilder("<script type=\"text/javascript\" language=\"javascript\" defer>");
if (msg != "")
js.AppendLine("alert('" + msg.ToString() + "');");
js.AppendLine("var ua = navigator.userAgent; var ie = navigator.appName==\"Microsoft Internet Explorer\"?true:false;");
js.AppendLine("if(ie){");
js.AppendLine("var IEversion = parseFloat(ua.substring(ua.indexOf(\"MSIE \")+5,ua.indexOf(\";\",ua.indexOf(\"MSIE \"))));");
js.AppendLine("if( IEversion< 5.5){");
js.AppendLine("var str = '';");
js.AppendLine("document.body.insertAdjacentHTML(\"beforeEnd\", str);");
js.AppendLine("document.all.noTipClose.Click();");
js.AppendLine("} else {");
js.AppendLine("window.opener =null; window.close();");
js.AppendLine("}");
js.AppendLine("}else{");
js.AppendLine("window.close();}");
js.AppendLine("</script>");
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), key, js.ToString());
}

/// <summary>
/// 控件点击消息确认提示框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
{
//Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
}

/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
{
StringBuilder Builder = new StringBuilder();
Builder.Append("<script type=\"text/javascript\" language=\"javascript\" defer>");
if (msg != "")
Builder.AppendFormat("alert('{0}');", msg);
Builder.AppendFormat("window.location.href='{0}';", url);
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "message", Builder.ToString());
}

/// <summary>
/// 输出自定义脚本信息
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="script">输出脚本</param>
public static void ResponseScript(System.Web.UI.Page page, string script)
{
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "message", "<script type=\"text/javascript\" language=\"javascript\" defer>" + script + "</script>");
}
/// <summary>
/// 输出自定义脚本信息
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="script">输出脚本</param>
public static void ResponseScript(System.Web.UI.Page page, string key, string script)
{
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), key, "<script type=\"text/javascript\" language=\"javascript\" defer>" + script + "</script>");
}

/// <summary>
/// 页面后退
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="count">后退步数</param>
public static void HistoryBack(System.Web.UI.Page page, int count)
{
StringBuilder Builder = new StringBuilder();
Builder.Append("<script type=\"text/javascript\" language=\"javascript\" defer>");
Builder.AppendFormat("window.history.go({0})", count);
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "historygo", Builder.ToString());
}

/// <summary>
/// 返回上一页
/// </summary>
/// <returns></returns>
public static void HistoryBack(System.Web.UI.Page page)
{
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "historygo", "<script type=\"text/javascript\" language=\"javascript\" defer>\n window.history.go(-1); \n </script>");
}

/// <summary>
/// js安全转换
/// </summary>
/// <param name="str">源字符</param>
/// <returns>返回代码</returns>
public static string StrSafe(string str)
{
StringBuilder rd = new StringBuilder(str);
rd.Replace("\b", "\\b");
rd.Replace("\f", "\\f");
rd.Replace("\n", "\\n");
rd.Replace("\r", "\\r");
rd.Replace("\t", "\\t");
rd.Replace("\'", "\\'");
rd.Replace("\"", "\\\"");
return rd.ToString();
}

#region Ajax
/// <summary>
/// 显示消息提示对话框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void AjaxShow(System.Web.UI.Page page, string msg)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "message", "alert('" + StrSafe(msg) + "');", true);
}
/// <summary>
/// 显示消息提示对话框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void AjaxShow(System.Web.UI.Page page, string key, string msg)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), key, "alert('" + StrSafe(msg) + "');", true);
}
/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
public static void AjaxShowAndRedirect(System.Web.UI.Page page, string msg, string url)
{
StringBuilder Builder = new StringBuilder();
if (msg != "")
Builder.AppendFormat("alert('{0}');", msg);
Builder.AppendFormat("window.location.href='{0}';", url);
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "message", Builder.ToString(), true);
}
/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
public static void AjaxShowAndRedirect(System.Web.UI.Page page, string key, string msg, string url)
{
StringBuilder Builder = new StringBuilder();
if (msg != "")
Builder.AppendFormat("alert('{0}');", msg);
Builder.AppendFormat("window.location.href='{0}';", url);
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), key, Builder.ToString(), true);
}
/// <summary>
/// 提示信息关闭页面,信息可为空不显示。
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息,为空则不提示</param>
public static void AjaxClose(System.Web.UI.Page page, string msg)
{
string js = "";
if (msg != "")
js += "alert('" + msg.ToString() + "');";
js += "window.close();";
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "message", js, true);
}
/// <summary>
/// 提示信息关闭页面,信息可为空不显示。
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息,为空则不提示</param>
public static void AjaxClose(System.Web.UI.Page page, string key, string msg)
{
string js = "";
if (msg != "")
js += "alert('" + msg.ToString() + "');";
js += "window.close();";
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), key, js, true);
}
/// <summary>
/// 输出自定义脚本信息
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="script">输出脚本</param>
public static void AjaxResponseScript(System.Web.UI.Page page, string script)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "message", script, true);
}
/// <summary>
/// 输出自定义脚本信息
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="script">输出脚本</param>
public static void AjaxResponseScript(System.Web.UI.Page page, string key, string script)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), key, script, true);
}
/// <summary>
/// 页面后退
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="count">后退步数</param>
public static void AjaxHistoryBack(System.Web.UI.Page page, int count)
{
StringBuilder Builder = new StringBuilder();
Builder.AppendFormat("window.history.go({0});", count);
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "historygo", Builder.ToString(), true);
}
/// <summary>
/// 返回上一页
/// </summary>
/// <returns></returns>
public static void AjaxHistoryBack(System.Web.UI.Page page)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "historygo", "window.history.go(-1)\";", true);
}
#endregion
}

posted @ 2018-05-10 10:53 幸福灬太奢侈 阅读(...) 评论(...) 编辑 收藏