mehmet6parmak
mehmet6parmak

Reputation: 4857

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

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

Upvotes: 2

Views: 193

Answers (2)

Nick Craver
Nick Craver

Reputation: 630587

You may want to checkout the ValidationSummary Control which is built for this, take a look at a quick demo here.

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" />

Upvotes: 2

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

Set the Display attribute on your validators to None.

Like:-

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

Upvotes: 2

Related Questions