0

我有一个例外,我需要显示一个消息框

我的消息框在本地主机上工作,但不在服务器上

catch (Exception)
        {

            MessageBox.Show("Machine Cannot Be Deleted", "Delete from other Places first", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

我怎样才能使这项工作...谢谢

有没有另一种方法可以做到这一点....请帮助..我知道这是一个小问题,但它需要做......

4

2 回答 2

8

您不能在 ASP.NET 中使用 Windows 窗体 MessageBox,因为它在服务器端运行,因此对客户端毫无用处。

考虑使用 Javascript 警报或其他类型的验证错误。(也许您的错误消息有一个隐藏控件,并在 catch 块中切换其可见性或使用 Response.Write 获取 Javascript 警报)。

像这样的东西(未经测试):

Response.Write("<script language='javascript'>window.alert('Machine Cannot Be Deleted, delete from other places first.');</script>");
于 2009-11-26T21:55:09.213 回答
0

您必须使用命名空间 System.Windows.Forms,然后您可以使用消息框属性

例如

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

**using System.Windows.Forms;**

    public partial class _Default : System.Web.UI.Page 
   {
      protected void Page_Load(object sender, EventArgs e)
       {
          MessageBox.Show("Machine Cannot Be Deleted", "Delete from other Places                   
          first", MessageBoxButtons.OK, MessageBoxIcon.Error);

       }    
    }

在其他替代方案中(除了布兰登先生提出的那个)

a) 使用 javascript

例如

Response.Write("<script>alert('Machine Cannot Be Deleted')</script>");

b)制作一个像消息框一样工作的自定义函数

例如

protected void Page_Load(object sender, EventArgs e)
    {
        MyCustomMessageBox("Machine Cannot Be Deleted");
    }

    private void MyCustomMessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
        Page.Controls.Add(lbl);
    }

希望这可以帮助

于 2009-11-29T07:11:17.313 回答