public class MessageBox
{
public enum MsgButton
{
/// <summary>
/// 只是OK按钮
/// </summary>
OK = 1,
/// <summary>
/// 提示是否确定
/// </summary>
OKCancel = 2
}
public enum GoBack
{
/// <summary>
/// 返回上一页面
/// </summary>
Yes = 1,
/// <summary>
/// 不返回
/// </summary>
No = 0
}
public static void Show(string msg)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');</script>");
}
public static void Show(string msg, MsgButton ConFirmButton)
{
switch (ConFirmButton)
{
case MsgButton.OK:
HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');</script>");
break;
case MsgButton.OKCancel:
HttpContext.Current.Response.Write("<script language='javascript'>confirm('" + msg + "')</script>");
break;
}
}
/// <summary>
/// 弹出消息
/// </summary>
/// <param name="msg">消息</param>
/// <param name="ConFirmButton">消息类型</param>
public static void Show(string msg, MsgButton ConFirmButton, GoBack Gbk)
{
string GobackScript = (Gbk == GoBack.Yes) ? "history.go(-1)" : "";
switch (ConFirmButton)
{
case MsgButton.OK:
HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');" + GobackScript + "</script>");
break;
case MsgButton.OKCancel:
HttpContext.Current.Response.Write("<script language='javascript'>confirm('" + msg + "')</script>");
break;
}
}
/// <summary>
/// 关闭窗口
/// </summary>
public static void ClosePage()
{
HttpContext.Current.Response.Write("<script> window.opener=null ;window.close(); </script>");
}
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="Msg">是否弹出信息</param>
public static void ClosePage(bool Msg)
{
if (Msg)
{
HttpContext.Current.Response.Write("<script> window.close(); </script>");
}
else
{
HttpContext.Current.Response.Write("<script> window.opener=null ;window.close();</script>");
}
}
public MessageBox()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 显示消息提示对话框
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void Show(System.Web.UI.Page page, string msg)
{
page.RegisterStartupScript("message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
}
/// <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 language='javascript' defer>");
Builder.AppendFormat("alert('{0}');", msg);
Builder.AppendFormat("top.location.href='{0}'", url);
Builder.Append("</script>");
page.RegisterStartupScript("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.RegisterStartupScript("message", "<script language='javascript' defer>" + script + "</script>");
}
}