分享一下在aspx页面弹框的设置代码
2018-06-22 07:57:31来源:未知 阅读 ()
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
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MVC异步分页代码分享 2020-02-21
- .aspx转换为.htm的方法 2019-10-18
- 分享基于EF6、Unitwork、Autofac的Repository模式设计 2019-07-23
- Swagger实例分享(VS+WebApi+Swashbuckle) 2019-07-23
- 记录未预编译文件“*.aspx”,因此不能请求该文件的几种处理 2019-04-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash