在客户端启用/禁用RequiredValidator / CustomValidator不触发 [英] Enable/disable RequiredValidator on client-side / CustomValidator not firing

查看:153
本文介绍了在客户端启用/禁用RequiredValidator / CustomValidator不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,用户可以在其中选择一个国家/地区。这是必填的字段。

I've got a drop-down where the user selects a Country. It is a required "field".

在它的旁边,有一个名为State的文本字段。如果用户选择US,则必须填写State。如果用户选择瑞典,因为瑞典没有州,所以不需要州。

Next to it, there is a textfield named State. If the user selects US, then the field State is required. If the user selects e.g. Sweden, the State is not required, since Sweden has no states.

示例代码:

<asp:DropDownList runat="server" ID="Country"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="Country"
                runat="server" Display="Static" ErrorMessage="Required field" />

<asp:TextBox runat="server" ID="State"></asp:TextBox>
<asp:CustomValidator ClientValidationFunction="DoesntGetFiredIfStateIsEmpty"
                runat="server" Display="Static" ErrorMessage="Required field" />

<!-- SO, RATHER THIS TOGETHER WITH CONDITIONAL FIRING -->
<asp:RequiredFieldValidator ControlToValidate="State"
                runat="server" Display="Static" ErrorMessage="Required field" />

我对您的问题是:当CustomValidator为空时,如何进行验证?

My question to you is: How can I make this CustomValidator fire validation when it is empty?

或更简单地说:如何有条件地使RequiredValidator开火?

Or put simplier: How can I make a RequiredValidator fire conditionally?

或者最简单:如何启用/禁用

Or simplest: How can I enable/disable a RequiredValidator on client-side?

推荐答案

尝试使用javascript来启用和禁用验证器

Try doing this with javascript to enable and disable validators

ValidatorEnable(RequiredFieldValidatorId, false);

查看我回答的问题

这篇关于在客户端启用/禁用RequiredValidator / CustomValidator不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆