在网站应用程序上显示错误/警告消息的最佳方式是什么?
我正在创建一个网站应用程序(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我更愿意这样做:-
I would prefer to do it in this way:-
对于服务器端验证,您可以像这样编写一个自定义控件(我就是这样做的),以在整个站点中一致地显示消息。
对于客户端验证,您可以使用验证摘要。
For server side validation, you can write a custom control (which I did) like this to display message consistantly through out the site.
For client side validation, you can use validation summary.
首先,您应该验证服务器端的所有内容,因为所有客户端机制都可以被规避。
显示错误消息的正常约定是将消息与包含无效信息的字段配对。
您还可以使用 HTML5 属性或 JavaScript 添加客户端验证 - 或者您可以将两者结合起来,但警告仍应与字段配对(并根据您的喜好设置样式)。
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).
老实说,这取决于您希望应用程序的工作方式。如果您想实时验证用户在订阅表单上的输入,那么您肯定不希望在离开文本框时出现 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.
伙计,
我喜欢 来自 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
请明确您要验证的内容。如果您正在验证包含用户名/密码的登录页面,那么显示“无效用户名/密码”的标签足以让用户轻松识别。为标签选择一种颜色,以便用户可以轻松注意到。
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.
你的问题对我来说有点不清楚。
无论如何,如果我们谈论一般验证:
您可以使用
JavaScript
或jQuery
,它们允许您进行客户端验证,这在当今已被广泛使用。或者您可以使用
asp.net
验证控件 就像 RequiredFieldValidator另外,请阅读有关 构建更好的 Web 表单:验证最佳实践 和 在 ASP.NET 中使用企业库验证应用程序块
You question is bit unclear for me.
Anyway if we talking about general validation:
You can use
JavaScript
orjQuery
which allow you to do client side validation, which is widely in use these days.OR you can use
asp.net
validation control like RequiredFieldValidatorAlso, please read this article about Building better web forms: Validation best practices and Using the Enterprise Library Validation Application Block in ASP.NET
有很多使用谷歌的网站对这个主题提供了很好的概述。 这里是一个很好的例子。
There are plenty of sites using google that give good overviews of this topic. here is one good example.