用户控制验证组问题 [英] User Control validation group issue

查看:96
本文介绍了用户控制验证组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面上的用户控件的两个实例。两者都有域和一个提交按钮。

I have two instances of a user control on a page. Both have fields and one submit button.

我已经设置上的字段和验证的验证组,但由于某种原因验证两个用户控件验证火灾时。

I have set validation groups on the fields and validators but for some reason when validating the two user controls' validators fire.

推荐答案

您可以暴露在你的用户控件的属性的ValidationGroup ,你会从页面设置。此值应存放在ViewState中,使用户控件的每个实例会得到不同的ValidationGroups(如果你的页面分配不同的)。

You could expose a property ValidationGroup in your UserControl that you would set from the Page. This value should be stored in ViewState, so that every instance of the UserControl will get different ValidationGroups(if your page assigns different).

例如:

Public Property ValidationGroup() As String
 Get
  Return CStr(ViewState("ValidationGroup"))
 End Get
 Set(ByVal value As String)
  SetValidationGroupOnChildren(Me, value)
  ViewState("ValidationGroup") = value
 End Set
End Property

Private Sub SetValidationGroupOnChildren(ByVal parent As Control, ByVal validationGroup As String)
    For Each ctrl As Control In parent.Controls
        If TypeOf ctrl Is BaseValidator Then
            CType(ctrl, BaseValidator).ValidationGroup = validationGroup
        ElseIf TypeOf ctrl Is IButtonControl Then
            CType(ctrl, IButtonControl).ValidationGroup = validationGroup
        ElseIf ctrl.HasControls() And ctrl.Visible = True Then
            SetValidationGroupOnChildren(ctrl, validationGroup)
        End If
    Next
End Sub


  • http://www.craigwardman.com/blog/index.php/2009/05/setting-a-validation-group-on-a-user-control/

  • HTTP://justgeeks.blogspot .COM / 2009/09 / BE-小心 - 使用 - 硬coded.html

    • http://www.craigwardman.com/blog/index.php/2009/05/setting-a-validation-group-on-a-user-control/
    • http://justgeeks.blogspot.com/2009/09/be-careful-using-hard-coded.html
    • 如果您需要在用户控件上的递归功能将不起作用不同ValidationGroups,那么你可以手动从codebehind分配给它。例如通过将用户控件的ID(就足够了)或客户端ID在根据控件的ValidationGroup属性前面。一个很好的地方,你可以调用这个函数会 preRender

      If you need different ValidationGroups in your UserControl the above recursive function won't work, then you could assign it manually from codebehind. For example by putting the UserControl's ID(might suffice) or ClientID in front of the ValidationGroup properties of the according controls. A good place where you could call this function would be PreRender.

      这篇关于用户控制验证组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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