Required Validator for Bound DropDownBox

2 posts, 0 answers
  1. Rick
    Rick avatar
    33 posts
    Member since:
    Aug 2012

    Posted 08 Dec 2011 Link to this post

    Hi, this is my second post about this since my first was not answered and then I guess removed.

    I am using MS Visual Studio Professional.  The code is ASP.net.  I am using Telerik's RadGrid (latest versions I assume, I don't know how to tell..)

    I am trying to add a Required validation to a bound Drop Down Box in the RadGrid.  I am thinking my problem is a syntax issue because I can get it to work for a regular text box, but the dropdown box just does not work.

    This is the code I use for the textbox which works (trying to use the code block format):
        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 work:
    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


  2. Milena
    Admin
    Milena avatar
    255 posts

    Posted 13 Dec 2011 Link to this post

    Hello Rick,

    You can find information on this matter here or in the last post in this forum.

    Best wishes,
    Milena
    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
Back to Top