如何在asp.net上显示MessageBox?

如果我需要在我的ASP.NET WebForm上显示MessageBox,该怎么做?

我试试: Messagebox.show("dd");

但它不起作用。

ASP.NET中不存在MessageBox。 如果您需要在浏览器中使用function,例如显示消息框,那么您需要选择javascript 。 ASP.NET为您提供了注入javascript的方法,当发送到浏览器的html被加载和显示时,javascript将被呈现和执行。 您可以在Page_Load中使用以下代码:

 Type cstype = this.GetType(); // Get a ClientScriptManager reference from the Page class. ClientScriptManager cs = Page.ClientScript; // Check to see if the startup script is already registered. if (!cs.IsStartupScriptRegistered(cstype, "PopupScript")) { String cstext = "alert('Hello World');"; cs.RegisterStartupScript(cstype, "PopupScript", cstext, true); } 

此示例取自MSDN 。

有简洁明了的方式:

 Response.Write(""); 

Messagebox仅适用于Windows。 你必须使用Javascript

 Alert('dd'); 

其中一个选项是使用Javascript。

以下是您可以从中开始的快速参考。

Javascript警报消息

Messagebox.show("dd");确实如此Messagebox.show("dd"); 不是使用System.Web;的一部分System.Web;

大部分时间我都有同样的感受。 如果要执行此操作,请执行以下步骤。

  • 右键单击解决方案资源管理器中的项目
  • 去添加引用,然后选择.NET选项卡

  • 然后选择,System.windows.forms(按’s’快速查找)

你可以获得命名空间,现在你可以使用Messagebox.show("dd");

但我建议使用javascript警报。

消息框仅默认可用于Windows窗体应用程序。如果要使用消息框资源,则必须使用“using system.windows.forms”来启用Web窗体模式的消息框。

你可以简单地写,但你必须使用JavaScript。

 Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", ""); 

如果需要,可以使用MessageBox ,但建议使用alert (来自JavaScript)。

如果你想使用它你应该写:

 System.Windows.Forms.MessageBox.Show("Test"); 

请注意,您必须指定命名空间。