.NET的消息框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET的消息框相关的知识,希望对你有一定的参考价值。

哦, 那个.NET中好像没有MessageBox("");消息框哦?
请问用什么代码可以代替这功能的?
这个不知行么:Response.Write("<script>alert(....);</script>");
还有别的么?

使用这个
public class MessageBox

private 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(page.GetType(), "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.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());


/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
/// <param name="frames">跳转的frames</param>
public static void ShowAndRedirectTo(System.Web.UI.Page page, string msg, string url, String frames)

StringBuilder Builder = new StringBuilder();
Builder.Append("<script language='javascript' defer>");
Builder.AppendFormat("alert('0');", msg);
Builder.AppendFormat("top.frames['" + frames + "'].window.location.href='0'", url);
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());


/// <summary>
/// 显示消息提示对话框,并关闭当前页面
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void ShowAndCloseWindow(System.Web.UI.Page page, string msg)

StringBuilder Builder = new StringBuilder();
Builder.Append("<script language='javascript'>");
Builder.AppendFormat("alert('0');", msg);
Builder.AppendFormat("window.close();");
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());


/// <summary>
/// 显示消息提示对话框,并返回到历史页面
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void ShowAndBackWindow(System.Web.UI.Page page, string msg)

StringBuilder Builder = new StringBuilder();
Builder.Append("<script language='javascript'>");
Builder.AppendFormat("alert('0');", msg);
Builder.AppendFormat("history.back();");
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());



/// <summary>
/// 打开新页面
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="msg">提示信息</param>
public static void OpenWindow(System.Web.UI.Page page, string url)

StringBuilder Builder = new StringBuilder();
Builder.Append("<script language='javascript'>");
Builder.AppendFormat("window.open('0');", url);
Builder.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "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(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");



参考技术A MessageBox.show("");消息框是在winform里面的,也可以添加引用里找到System.Windows.Forms,然后在cs类中添加using System.Windows.Forms就可以有了,不过这个对话框是弹出在网页后面的,不好用.
如果asp中想弹出就还是Response.Write("<script>alert(....);</script>"); 吧.
下面是一个弹出确认对话框的方法;
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
参考技术B winform 里面的弹出是:MessageBox("");
webform 里面的弹出: RegisterClientScriptBlock 是执行客户端的JavaScript代码,可以用于弹出。
Response.write("<script>alert('OK');</script>");
也是执行客户端的JavaScript代码
参考技术C string ConfirmContent = "<script>alert('。。。。。'); </script>";
Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler;
ParameterPage.RegisterClientScriptBlock("confirm", ConfirmContent);本回答被提问者采纳

powershell PowerShell .NET消息框

[System.Windows.Forms.MessageBox]::Show("The computer name you specified was not valid.", "Computer Name not valid")

以上是关于.NET的消息框的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET点击提交按钮时弹出一个消息框,但是不想把fileupload控件的路径清空改怎么办?

无法使用 SQL 数据激活消息框 - ASP.NET

ASP.NET Web 应用程序消息框

csharp asp .net c#消息框

powershell PowerShell .NET消息框

ASP页面即时提示消息框代码