在 ASP.NET 中使用 ValidationSummary 时如何不显示 BaseValidator.Text
我在 Web 表单上使用了一堆不同的 asp.net 验证控件。 其中一些的 Text 属性设置为“* 错误”或“您错过了这些字段”之类的内容。
但是,一些 CustomValidator 控件具有空白 Text 属性。 我故意将它们留空,因为我根据失败的情况动态添加 ErrorMessage。 (CustomValidator 可能有许多不同的条件,我根据这些条件设置 args.IsValid = false)
当发生错误时,我设置的 ErrorMessage 属性同时显示在 ValidationSummary 和 Validator 控件内。 我不想要这样。 我希望能够仅在 ValidationSummary 中显示 ErrorMessage,而不是在 BaseValidator.Text 属性中显示。
我的第一次尝试是将 Text 属性设置为空格“”。 那行不通。
我(目前)实现的是一个显示为与背景颜色相同的周期。 这是一种黑客行为——我不喜欢它。 哎呀,也许这就是我来这里的原因!
代码如下:
<asp:CustomValidator ID="StackOverflowValidator" runat="server"
Text="."
CssClass="validatorstyle"
Display="Dynamic"
OnServerValidate="validate_AllowedToDoSomething"
ValidationGroup="MainGroup" />
<asp:ValidationSummary ID="mainGroupValidationSummary" runat="server"
ValidationGroup="MainGroup"
DisplayMode="BulletList"
HeaderText="There was an error in saving. Please check the following:" />
在 validate_AllowedToDoSomething 内,我调用:
StackOverflowValidator.ErrorMessage = "Custom Error Message #1";
args.IsValid = false;
return;
我在 Web 表单上得到的是“自定义错误消息 #1”两次。 提前致谢!
I'm using a bunch of different asp.net validation controls on a web form. Some of these have their Text property set to things like "* Error" or "You missed these fields".
However, a few of the CustomValidator controls have blank Text properties. I left them blank on purpose because I'm adding the ErrorMessage dynamically depending on which case fails. (A CustomValidator may have many different conditions upon which I set args.IsValid = false)
When an error occurs, the ErrorMessage property that I set is shown both in the ValidationSummary and inside the Validator control. I don't want that. I want to be able to just show the ErrorMessage in the ValidationSummary and not in the BaseValidator.Text property.
My first try was to set the Text property to be a space " ". That didn't work.
What I implemented (for now) is a period that is shown as the same color of the background. It's a hack - and I don't like it. Heck, maybe that's why I'm here!
Here's the code:
<asp:CustomValidator ID="StackOverflowValidator" runat="server"
Text="."
CssClass="validatorstyle"
Display="Dynamic"
OnServerValidate="validate_AllowedToDoSomething"
ValidationGroup="MainGroup" />
<asp:ValidationSummary ID="mainGroupValidationSummary" runat="server"
ValidationGroup="MainGroup"
DisplayMode="BulletList"
HeaderText="There was an error in saving. Please check the following:" />
Inside validate_AllowedToDoSomething I call:
StackOverflowValidator.ErrorMessage = "Custom Error Message #1";
args.IsValid = false;
return;
What I get is "Custom Error Message #1" twice on the web form. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 BaseValidator 上设置
display="none"
而不是"dynamic"
即可解决问题。Just set
display="none"
instead of"dynamic"
on the BaseValidator, and that should solve it.