简体   繁体   中英

Required Field Validator for 3 Controllers

Each Required Field Validator can validate 1 Controller, but let's say I have 3 DropDownLists.

Date of Birth:

-Day- -Month- -Year-

Now I know that I can validate 1 DropDownList by setting the InitialValue, where the first Item in the DropDownList is "Select a Day", which has a Value="".

But I do not want to use 3 validators. (Day is missing, Month is missing, Year is missing)

I wish to use 1 Validator for all 3, means, if I set the day and the month but I don't set the year, I get (Date must be set), same thing If I set the day and the year without the month..etc...

I know that I can do this with a custom validator, but can I do it with 1 Required Field Validator? Because usually Custom validators requires the page to be refreshed (so it can execute server side codes).

You can use validation summary

 <asp:ValidationSummary ID="ValidationSummary1"
  HeaderText="Date must be set"
    DisplayMode="BulletList"
     EnableClientScript="true"
       runat="server"/>

And remove the ErrorMessage ,InitialValue and Text properties of required field validator of dropdowns. This will give you required output.

I don't think you can accomplish this with just one RequiredFieldControl, either you use Custom Validator, or in the code-behind iterate over each control and see that its index it's not zero (assuming that that's the initial value you want to avoid)

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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