我想在 MessageBox 中显示我的验证错误消息。我有四个文本框和一个按钮控件。当我单击 Button 控件时,没有文本的 TextBoxes 将显示在 MessageBox 中。我几乎完成了这个,但问题是当我单击按钮时,消息框作为最小化窗口打开。所以最终用户很难实现。我想在单击按钮时向用户显示 MessageBox。
这是我的代码,在按钮点击事件中
ErrorMsg="";
if (TextBox1.Text == "")
{
ErrorMsg += "Name is required!";
ErrorMsg += "\n";
}
if (TextBox2.Text == "")
{
ErrorMsg += "Address is required!";
ErrorMsg += "\n";
}
if (TextBox3.Text == "")
{
ErrorMsg += "Phone No. is required!";
ErrorMsg += "\n";
}
if (TextBox4.Text == "")
{
ErrorMsg += "City is required!";
ErrorMsg += "\n";
}
if (ErrorMsg.Length == 0)
{
//Some Code
}
else
{
MessageBox.Show(ErrorMsg, "Existing Address", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
提前致谢...
最佳答案
您不能在 ASP.NET 应用程序中使用 Windows MessageBox。它只是没有意义。 ASP.NET 吐出 HTML/javascript/CSS,因此您应该使用它来通知用户出现问题。因此,例如在 ASP.NET 应用程序中,您可以使用 RegisterStartupScript
将 javascript 注入(inject)页面的方法,该页面将在加载页面时执行,并使用 alert
函数:
ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('oops');", true);
在 ASP.NET 应用程序中执行验证的其他更传统的技术涉及使用 validation controls .
关于c# - 如何在 ASP.NET Web 应用程序中显示 Windows.Form.MessageBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779576/