简体   繁体   English

3个控制器的必需字段验证器

[英]Required Field Validator for 3 Controllers

Each Required Field Validator can validate 1 Controller, but let's say I have 3 DropDownLists. 每个必需字段验证器可以验证1个控制器,但是假设我有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="". 现在我知道我可以通过设置InitialValue来验证1 DropDownList,其中DropDownList中的第一个Item是“Select a Day”,其值为“”。

But I do not want to use 3 validators. 但我不想使用3个验证器。 (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... 我希望对所有3使用1个Validator,意思是,如果我设置了日期和月份,但是我没有设置年份,我得到(日期必须设置),同样的事情如果我设置了没有的日期和年份month..etc ...

I know that I can do this with a custom validator, but can I do it with 1 Required Field Validator? 我知道我可以使用自定义验证器执行此操作,但是我可以使用1必填字段验证器吗? 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. 并删除下拉列表所需字段验证器的ErrorMessage,InitialValue和Text属性。 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) 我不认为只用一个RequiredFieldControl就可以完成这个任务,你要么使用自定义验证器,要么在每个控件的代码隐藏迭代中看到它的索引不是零(假设这是你要避免的初始值)

Good luck 祝好运

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

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