在网站应用上显示错误/警告消息的最佳方法是什么?

时间:2011-10-28 12:15:13

标签: c# asp.net label messagebox

我正在创建一个网站应用程序(ASP.NET,C#),我只想知道显示错误/警告消息的最佳方式。是通过MessageBox还是通过Label更好?只需要一些建议。

9 个答案:

答案 0 :(得分:2)

对于服务器端验证,您可以编写这样的自定义控件(我这样做),以便在整个站点中一致地显示消息。

enter image description here

对于客户端验证,您可以使用验证摘要。

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List"
    ShowMessageBox="true" ShowSummary="false" />

答案 1 :(得分:2)

我更愿意这样做: -

 this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);

答案 2 :(得分:1)

你的问题对我来说有点不清楚。

无论如何,如果我们谈论一般验证:

您可以使用JavaScriptjQuery来进行客户端验证,这些验证目前已广泛使用。

或者您可以asp.net使用{{1}} validation control

另外,请阅读有关RequiredFieldValidatorBuilding better web forms: Validation best practices

的文章

答案 3 :(得分:1)

首先,您应该验证服务器端的所有内容,因为可以规避所有客户端机制。

显示错误消息的常规约定是将消息与包含无效信息的字段配对。

您还可以使用HTML5属性或JavaScript添加客户端验证 - 或者您可以将两者结合使用,但警告仍应与字段配对(无论您喜欢哪种样式)。

<label>First name<br>
<input type="text" name="firstname" required></label>
<span class="error">You must enter a first name</span>

答案 4 :(得分:1)

老实说,这取决于您希望应用程序的工作方式。如果您想要实时验证用户输入,例如订阅表单,那么当您离开文本框时,您肯定不希望出现javascript警报。所以在这种情况下,我更喜欢Sohnee描述的内联方式。

但是如果你想显示应用程序关键操作的失败消息,我会使用javascript警报,或者如果回发事件引发警告(在后面的代码中),你可以将它们写在容器中(a div或so)在呈现页面时可见。

答案 5 :(得分:1)

曼,

我喜欢Bootstrap from twitter,这是一个时尚,直观且功能强大的前端框架,可以更快,更轻松地进行Web开发。

[]的

答案 6 :(得分:1)

请明确您要验证的内容。如果您要验证包含用户名/密码的登录页面,则显示“无效用户名/密码”的标签足以让用户轻松识别。为用户可以轻松通知的标签添加颜色。

答案 7 :(得分:1)

There are many ways you can display the error message.

1)Simple Message Box.You may need add System.Windows as namespace in application.

2)The same message box look and feel you can create using the below code,

this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);

3)Using InBuilt Asp.net validation control like Required Field Validator,RangeValidator,Validatio Summary.

4)Place a Label control on each page,use it as when required to display error msg.

5)Define a validation class,create rules and error message commonly used across apllications.

6)Using Javascript prompt and alert also,you can display it.

答案 8 :(得分:-1)

有很多使用谷歌的网站都对这个主题进行了很好的概述。 here就是一个很好的例子。

相关问题