Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Control validation group issue

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.

like image 882
Burt Avatar asked Jun 22 '11 16:06

Burt


People also ask

What are the five types of validation controls explain?

The RangeValidator control verifies that the input value falls within a predetermined range. It defines the type of the data. The available values are: Currency, Date, Double, Integer, and String.

What is group validation?

Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page. You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group.

What is the validation group in asp net?

Validation groups allow you to validate data entry controls in groups. Server controls such as validation controls, Button and TextBox have ValidationGroup property that takes a string value.


2 Answers

This method also works:

Dim valGroup = String.format("{0}-validation", Guid.NewGuid())

rfv001.ValidationGroup = valGroup
rfv002.ValidationGroup = valGroup
rfv003.ValidationGroup = valGroup
rfv004.ValidationGroup = valGroup
rfv005.ValidationGroup = valGroup

btnSubmit.ValidationGroup = valGroup

You only need to set the values for the ValidationGroup manually.

like image 141
Marcellino Bommezijn Avatar answered Nov 27 '22 19:11

Marcellino Bommezijn


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).

For example:

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

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.

like image 21
Tim Schmelter Avatar answered Nov 27 '22 19:11

Tim Schmelter