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

6

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


5
什么类型的信息?验证信息?页面未找到错误?请更具体地说明。 - Yurii Hohan
3
我认为这个问题并不值得被下降两个赞,它是一个完全有效的问题。 - Mantorok
2
@Googler - 只是提醒一下,虽然问题是合法的,但很多人已经给它投了反对票。请更具体地描述你的问题,并在找到你要寻找的答案时将其标记为答案。 :) - Win
9个回答

2

我更倾向于以这种方式来实现:

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

2

对于服务器端验证,您可以编写一个自定义控件(我已经完成了),如下所示,以便在整个站点中一致地显示消息。

enter image description here

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

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

1
首先,您应该在服务器端验证所有内容,因为所有客户端机制都可以被规避。
显示错误消息的常规约定是将消息与包含无效信息的字段配对。
您还可以使用HTML5属性或JavaScript添加客户端验证,或者将两者结合使用,但警告仍应与字段配对(并以任何您喜欢的样式进行设置)。
<label>First name<br>
<input type="text" name="firstname" required></label>
<span class="error">You must enter a first name</span>

1

伙计,

我喜欢Twitter的Bootstrap,它是一个时尚、直观和强大的前端框架,可以加快和简化网站开发。

祝好,


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

1

1
说实话,这取决于您希望应用程序的工作方式。如果您想在订阅表单上实时验证用户输入,那么当您离开文本框时,绝对不希望出现JavaScript警报。因此,在这种情况下,我更喜欢Sohnee所描述的内联方式。
但是,如果您想显示应用程序关键操作的失败消息,我会选择JavaScript警报,或者如果后端代码引发警告(在代码后面),则可以将这些警告写入容器(例如div),在页面呈现时将其显示出来。

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.

-1

有很多使用谷歌的网站可以给出这个主题的良好概述。这里是一个很好的例子。


czuroski:异常处理如何成为这个问题的一个很好的例子? - huMpty duMpty
我可能误解了问题。我假设这个问题是在寻找一种处理异常并在必要时向用户显示它们的方法。 - czuroski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接