CustomValidator 用于验证多个控件

发布于 2024-09-01 07:39:40 字数 170 浏览 1 评论 0原文

有没有一种方法可以让自定义验证器从两个控件(例如两个复选框)获取输入并确保只选中一个?

或者我的做法是错误的,更重要的是,如果我是错误的,你能给我指出正确的方向吗?

即使这实际上是错误的方向,我也想知道如何做到这一点,以防我遇到适用的情况,即如果可能的话。

感谢您提前提供帮助。

Is there a way to have the custom validator take the input from two controls, say for example two check boxes and make sure that only one is checked?

Or am I going about this all wrong, and more importantly if I am could you point me in the right direction?

Even if this is the wrong direction actually I would like to know how to do this in case I run into a situation where it is applicable, that is if it's possible.

Thanks for you help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2024-09-08 07:39:40

如果您想确保仅选中两个复选框之一,则您使用了错误的控件。

使用单选按钮设置独家选择。

If you want to make sure that only one of two checkboxes are checked, you are using the wrong control.

Use a radio button to setup an exclusive selection.

情归归情 2024-09-08 07:39:40

AJAX 控制工具包有一个专有的复选框列表。我同意它的必要性,因为单选按钮不能像复选框那样取消选择。

如果您想要滚动自定义验证器,听起来您要么在服务器端进行,此时您可以在后面的代码中执行您想要的任何逻辑。相反,您可以使用自己的 javascript 来强制执行它。

The AJAX Control Toolkit has an exclusive checkbox list. I agree with the need for it, as radio buttons cannot be de-selected like checkboxes can.

If you're looking to roll a custom validator, it sounds like you either do it server side, at which point you can do whatever logic you want in the code behind. Conversely, you can roll your own javascript to enforce it.

┈┾☆殇 2024-09-08 07:39:40

我同意米切尔的观点,即单选按钮似乎更合适。

但是您是否尝试过连接 CompareValidator 并将其设置为验证 UnEqual?

I agree with Mitchel that a RadioButton seems more appropriate.

But have you tried hooking up a CompareValidator and set it to validate UnEqual?

饮惑 2024-09-08 07:39:40

我也在研究类似的问题。我解决这个问题的方法是为每个控件创建一个验证器,并将它们链接到相同的方法,以确保您的验证限制是什么。

protected void validator1_ServerValidate(...){
  myValidationMethod(...);
}

protected void validator2_ServerValidate(...){
  myValidationMethod(...);
}

它有效,但在我看来它非常丑陋

I'm also working on a similar problem. The way I worked around this issue, was to create a validator for each of my controls, and to link them to the same method that ensures whatever your validation restrictions are.

protected void validator1_ServerValidate(...){
  myValidationMethod(...);
}

protected void validator2_ServerValidate(...){
  myValidationMethod(...);
}

It works, but it's pretty ugly IMO

夏了南城 2024-09-08 07:39:40

我写了一篇关于这件事的博客文章。

结合客户端和服务器端验证并重新验证

I wrote a blog article about this very thing.

http://coding.infoconex.com/post/2010/10/11/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting

Combines both Client Side and Server Side validation and re-validates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文