如何为多个RequiredFieldValidator仅显示一条消息?
如何仅显示多个 RequiredFieldValidator
的单个消息,而不是 RequiredFieldValidator
的单个消息?
我想要如下图所示..
我的观点是..
< img src="https://i.sstatic.net/kpk9j.png" alt="我的观点是。">
how to display only single message for multiple RequiredFieldValidator
instead of individual message for RequiredFieldValidator
?
i want to as shown in following image..
my view is..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为此,您必须使用
ValidationSummary
控件。请参阅这篇 ValidationSummary 类 MSDN 文章有关如何执行此操作的详细信息和示例。本文包含您想要准确弄清楚的示例。You will have to use
ValidationSummary
control for this. See this ValidationSummary Class MSDN article for details and example on how to do this. This article contains an example of what you are trying to figure out exactly.将 HeaderText 设置为验证摘要中的“(*) 必填字段”之类的内容。
set the HeaderText to something like "(*) Fields are required" to your validation summary.
您可以将每个RequiredFieldValidator 的错误消息字段留空,并将* 放入文本字段中,然后添加ValidationSummary,使用错误消息定义其标题文本,这将适用于您的场景。
you can leave error message field of each RequiredFieldValidator blank and put * in text field, then add ValidationSummary defining its header text with error msg this will works for your scenario.
这位先生在这里很简单地解决了这个问题: http://www.cactusoft.com/blog_40
This gentleman solved it here quite simply: http://www.cactusoft.com/blog_40
快速简单的方法:将 CssClass 添加到 ValidationSummary,然后添加一个 css 样式,该样式设置该类下的 ul 元素以显示:none。
例如:
...
Quick and easy way: add a CssClass to the ValidationSummary, then a css style that sets ul elements under that class to display: none.
For example:
...
我可以看到您想要做什么,但是使用 ASP.Net 验证器很难
我能想到的唯一方法是完全删除 ValidationSummary 并使用 ASP.Net 验证器 API 和 JQuery 手动创建您自己的验证器,即
ErrorMessage = "*"
删除文本值Page_IsValid
的内容来自 ASP.Net 验证器 API。如果页面验证失败,则设置为 false。当然,这假设您的表单上只有必需的字段验证器。如果存在混合,那么您将需要通过使用 JQuery/javascript 迭代客户端上的
Page_Validators
来检查一个或多个必需的验证是否失败。老实说,尽管我不会这样做 - 它是太难了,
我就这样做 - 对于每个必填字段验证器 - 设置
I can see what you are trying to do but it's difficult with the ASP.Net validators
The only way I can think to do it is remove the ValidationSummary altogether and manually create your own using the ASP.Net validator API and JQuery i.e.
ErrorMessage = "*"
Remove Text valuePage_IsValid
is from the ASP.Net validator API. Set to false if the page fails validation.Of course this assumes that you only have the required field validators on your form. If there is a mix then you will need to check if one or more of the required ones have failed by iterating through the
Page_Validators
on the client using JQuery/javascriptHonestly though I wouldn't do it - it's too hard
I would just do this - For each required field validator - set
除了 ValidationSummary 之外,您还应该使用 ASP.NET 中的 ValidationSummary 控件,您还可以使用 Group 属性将控件分成逻辑组。请参阅 http://msdn.microsoft.com/ en-us/library/system.web.ui.webcontrols.validationsummary.aspx 获取大量示例。
You should use the ValidationSummary Control from ASP.NET in addition to ValidationSummary you could also use the Group property to separate controls into logical groups. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.aspx for a bunch of examples.
使用 ValidationSummary
ValidationSummary 控件用于显示网页中发生的所有验证错误的摘要。
此控件中显示的错误消息由每个验证控件的 ErrorMessage 属性指定。如果未设置验证控件的 ErrorMessage 属性,则不会显示该验证控件的错误消息。
http://asp-net-example .blogspot.in/2008/10/validationsummary-example-how-to-use.html
use ValidationSummary
The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page.
The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.
http://asp-net-example.blogspot.in/2008/10/validationsummary-example-how-to-use.html
要添加到 Mike Godin 的答案,仅显示多个字段验证器的单个警报消息:
保留单独的必需消息。添加验证摘要,其中 DisplayMode="BulletList" 和 HeaderText="请提供上述所需信息。"
“BulletList”显示模式在验证摘要 DIV 内生成 LI 的无序列表,然后通过样式隐藏 UL - 只有“HeaderText”会显示:
To add to Mike Godin's answer, for only showing a single alert message for multiple field validators:
Keep individual required messages. Add Validation Summary with DisplayMode="BulletList" and HeaderText="Please provide the required information above."
The "BulletList" display mode produces a unordered list of LI's inside the Validation Summary DIV, then hide UL via styling - only the "HeaderText" will show: