This is a migrated thread and some comments may be shown as answers.

Drop Down Box - Required Validation

1 Answer 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick
Top achievements
Rank 1
Rick asked on 06 Dec 2011, 10:08 PM
I am trying to add a Required validation to a bound Drop Down Box.  I have code that does this for a text box and it works, but it doesn't work for a Drop Down box.

This Works:

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated


        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then


            Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim editor As GridTextBoxColumnEditor = DirectCast(item.EditManager.GetColumnEditor("CurrencyTarget_NativeCurrent"), GridTextBoxColumnEditor)
            Dim cell As TableCell = DirectCast(editor.TextBoxControl.Parent, TableCell)
            Dim validator As New CompareValidator ' RequiredFieldValidator()


            editor.TextBoxControl.ID = "NCReqValidator"
            validator.ControlToValidate = editor.TextBoxControl.ID
            validator.Operator = ValidationCompareOperator.GreaterThanEqual
            validator.ValueToCompare = "0"
            validator.ErrorMessage = "* Native Current is a required field"
            cell.Controls.Add(validator)

        End If
End Sub

But when I add the same code for a drop down box, it does not:

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated


        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim item2 As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim editor2 As GridDropDownListColumnEditor = DirectCast(item2.EditManager.GetColumnEditor("ddCountryCode"), GridDropDownListColumnEditor)
            Dim cell2 As TableCell = DirectCast(editor2.DropDownListControl.Parent, TableCell)
            Dim validator2 As New CompareValidator ' RequiredFieldValidator()

            editor2.DropDownListControl.ID = "CCReqValidator"
            validator2.ControlToValidate = editor2.DropDownListControl.ID
            validator2.Operator = ValidationCompareOperator.NotEqual
            validator2.ValueToCompare = "XXX"
            validator2.ErrorMessage = "* Country Code is a required field"
            cell2.Controls.Add(validator2)

        End If


    End Sub

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 09 Dec 2011, 03:23 PM
Hi Rick,

Check out the forum thread linked below and see if it helps:
http://www.telerik.com/community/forums/aspnet-ajax/grid/grid-dropdown-list-validation-error.aspx

Best wishes,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Rick
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or