简体   繁体   English

ASP.NET验证控件

[英]ASP.NET validation controls

I want to use Validation Controls but I dont want them to show their Error Messages when invalid data exist. 我想使用验证控件,但我不希望它们在存在无效数据时显示其错误消息。 Instead I'm going to iterate through the validation controls and show error messages inside my little ErrorMessage Control 相反,我将遍历验证控件并在我的小ErrorMessage控件中显示错误消息。

for (int i = 0; i < Page.Validators.Count; i++)
        {
            if (!Page.Validators[i].IsValid)
            {
                divAlert.InnerText = Page.Validators[i].ErrorMessage;
                return false;
            }                
        }

I'm doing this because i have little space to show the error message. 我这样做是因为我几乎没有空间显示错误消息。 You can ask why are you using validation control if you dont want to show them My asnwer is "I use them for validation logic they handle" 您可以问为什么不想使用验证控件,如果我不想显示给他们,我的回答是“我将它们用于它们处理的验证逻辑”

I looked the properties of the validation controls and cant find something that wil help me doing this. 我查看了验证控件的属性,找不到可以帮助我完成此操作的内容。

Any Idea? 任何想法?

Thanks 谢谢

Set the Display attribute on your validators to None. 将验证器上的“显示”属性设置为“无”。

Like:- 喜欢:-

<asp:RegularExpressionValidator ID="MyValidator" runat="server" Display="None" />

You may want to checkout the ValidationSummary Control which is built for this, take a look at a quick demo here . 您可能想签出为此而构建的ValidationSummary控件在此处查看快速演示

For example this shows just a red asterisk at the field, but a full field name in the summary: 例如,这在字段上仅显示一个红色星号,但摘要中显示完整的字段名称:

 <asp:RequiredFieldValidator ControlToValidate="myControl" runat="server"
  ErrorMessage="Summary Description" Text="*" InitialValue="" />
 <asp:ValidationSummary runat="server"
  HeaderText="You must enter a value in the following fields:"
  DisplayMode="BulletList" EnableClientScript="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM