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

发布于 2024-12-12 13:35:35 字数 90 浏览 1 评论 0原文

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

I'm creating a website app(ASP.NET, C#) and I just want to know the best way to display error/warning messages. Is it better to do it by MessageBox or thru a Label? Just need some suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

狼亦尘 2024-12-19 13:35:35

我更愿意这样做:-

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

I would prefer to do it in this way:-

 this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);
も星光 2024-12-19 13:35:35

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

在此处输入图像描述

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

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

For server side validation, you can write a custom control (which I did) like this to display message consistantly through out the site.

enter image description here

For client side validation, you can use validation summary.

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List"
    ShowMessageBox="true" ShowSummary="false" />
咆哮 2024-12-19 13:35:35

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

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

您还可以使用 HTML5 属性或 JavaScript 添加客户端验证 - 或者您可以将两者结合起来,但警告仍应与字段配对(并根据您的喜好设置样式)。

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

Firstly, you should be validating everything server side, because all client side mechanisms can be circumvented.

The normal convention for displaying error messages is to pair the message with the field that contains the invalid information.

You can also add client-side validation using HTML5 attributes, or JavaScript - or you can combine the two, but the warnings should still be paired with the field (and styled however you like).

<label>First name<br>
<input type="text" name="firstname" required></label>
<span class="error">You must enter a first name</span>
江城子 2024-12-19 13:35:35

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

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

Honestly it depends on the way you want your application to work. If you want to real-time validate user input on for instance a subscription form, you'd definitely don't want javascript alerts when you leave a textbox. So in that case I'd prefer the inline way that Sohnee describes.

But if you want to show the failure message of a application-critical action, I'd go with a javascript alert, or if a postback event raises warnings ( in the code behind ) you could write these in a container (a div or so) which will be visible when the page is rendered.

べ映画 2024-12-19 13:35:35

伙计,

我喜欢 来自 twitter 的 Bootstrap,这是一个时尚、直观且功能强大的工具用于更快、更轻松的 Web 开发的前端框架。

[]的

Man,

I like Bootstrap from twitter, that is a sleek, intuitive, and powerful front-end framework for faster and easier web development.

[]'s

穿越时光隧道 2024-12-19 13:35:35

请明确您要验证的内容。如果您正在验证包含用户名/密码的登录页面,那么显示“无效用户名/密码”的标签足以让用户轻松识别。为标签选择一种颜色,以便用户可以轻松注意到。

Please be clear on what you want to validate on. If you are validating a login page which contains the username/Password, then a label displaying 'Invalid username/Password' is well enough for a user to recognize easily. Have a color for the label where user could easily notify.

天涯沦落人 2024-12-19 13:35:35
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.
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.
甜心小果奶 2024-12-19 13:35:35

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

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

您可以使用 JavaScriptjQuery,它们允许您进行客户端验证,这在当今已被广泛使用。

或者您可以使用 asp.net 验证控件 就像 RequiredFieldValidator

另外,请阅读有关 构建更好的 Web 表单:验证最佳实践在 ASP.NET 中使用企业库验证应用程序块

You question is bit unclear for me.

Anyway if we talking about general validation:

You can use JavaScript or jQuery which allow you to do client side validation, which is widely in use these days.

OR you can use asp.net validation control like RequiredFieldValidator

Also, please read this article about Building better web forms: Validation best practices and Using the Enterprise Library Validation Application Block in ASP.NET

屌丝范 2024-12-19 13:35:35

有很多使用谷歌的网站对这个主题提供了很好的概述。 这里是一个很好的例子。

There are plenty of sites using google that give good overviews of this topic. here is one good example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文