解决方法
您可以在UserControl中公开要从Page设置的属性ValidationGroup.此值应存储在ViewState中,以便UserControl的每个实例都将获得不同的ValidationGroup(如果您的页面分配不同).
例如:
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-careful-using-hard-coded.html
如果在UserControl中需要不同的ValidationGroup,则上述递归函数将不起作用,然后您可以从代码隐藏中手动分配它.例如,将UserControl的ID(可能已足够)或ClientID放在相应控件的ValidationGroup属性之前.你可以调用这个函数的好地方是PreRender.