我有一个例外,我需要一个消息框
我的消息框适用于localhost但不适用于服务器
catch (Exception)
{
MessageBox.Show("Machine Cannot Be Deleted", "Delete from other Places first", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我怎样才能完成这项工作......谢谢
还有另一种方法可以做到这一点....请帮助..我知道这是一个小问题,但需要做...
答案 0 :(得分:8)
您不能在ASP.NET中使用Windows窗体MessageBox,因为它在服务器端运行,使其对客户端无用。
查看使用Javascript警报或其他类型的验证错误。 (可能对您的错误消息有一个隐藏控件,并在catch块中切换其Visibility或使用Response.Write获取Javascript警报)。
像这样(未经测试):
Response.Write("<script language='javascript'>window.alert('Machine Cannot Be Deleted, delete from other places first.');</script>");
答案 1 :(得分:0)
您必须使用命名空间 System.Windows.Forms
,然后才能使用消息框属性
e.g。
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
e.g。
Response.Write("<script>alert('Machine Cannot Be Deleted')</script>");
b)制作一个像消息框一样的自定义功能
e.g。
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);
}
希望这有帮助