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

Programmatically adding RequiredFieldValidator

3 Answers 211 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve Wolfe
Top achievements
Rank 1
Steve Wolfe asked on 17 Dec 2009, 08:59 PM
I have been trying to programmatically add a RequiredFieldValidator to an auto-generated GridDropDownColumn, but have been unsuccessful.  

Has anyone tried doing this, and if so could you provider any pointers.

I have 2 use cases:

  1. Dropdown is pre-populated with values.  First being a "Please select a choice" item.   By not setting a selected value this is the default value, and I want to verify that a different choice has been made.
  2. Dropdown list has no items in it, and I do not want to allow an insert/update to happen in this case.

Thanks in advanced for any help.

-Steve

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Dec 2009, 05:35 AM
Hello Steve,

Here is the code that I tried to add RequireFieldValidator for GridDropDownColumn. Give a try with this.
CS:
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            GridEditableItem item = e.Item as GridEditableItem; 
            GridDropDownColumnEditor editor = (GridDropDownColumnEditor)item.EditManager.GetColumnEditor("Dropdown"); 
            TableCell cell = (TableCell)editor.ContainerControl; 
 
            RequiredFieldValidator validator = new RequiredFieldValidator(); 
            editor.ContainerControl.ID = "ID_for_validation"
            validator.ControlToValidate = editor.ContainerControl.Controls[0].ID; 
            validator.InitialValue = "Please Select a Choice"
            validator.ErrorMessage = "*"
            cell.Controls.Add(validator); 
        } 
    } 

Also checkout the following help documentation for more information on this:
Validation

-Shinu.
0
Steve Wolfe
Top achievements
Rank 1
answered on 18 Dec 2009, 06:55 AM
Shinu,

Thanks for the reply.  Yes, this works.   You have to use the "Text" of the selected item.  Each item in the dropdown list has 2 "values", the text and the value.  I was misinterpreting InitialValue on the Validator as the "value" of the dropdown list item.

-Steve
0
misha
Top achievements
Rank 1
answered on 25 Feb 2014, 08:09 PM
Thanks for pointing that out.. I too was making the same mistake.
Tags
Grid
Asked by
Steve Wolfe
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Steve Wolfe
Top achievements
Rank 1
misha
Top achievements
Rank 1
Share this question
or