I have a drop-down list that I am dynamically developing in code.
Dim objPreferenceDropdownList As DropDownList = New DropDownList() objPreferenceDropdownList.ID = "objPreferenceDropdownList" objPreferenceDropdownList.AppendDataBoundItems = "True" objPreferenceDropdownList.AutoPostBack = True
I fill this drop-down menu with various elements such as
objPreferenceDropdownList.Items.Add(new ListItem("--Select Color--","0")); objPreferenceDropdownList.Items.Add(new ListItem("Red","1")); objPreferenceDropdownList.Items.Add(new ListItem("Blue","2")); objPreferenceDropdownList.Items.Add(new ListItem("White", "3")); objPreferenceDropdownList.Items.Add(new ListItem("Pink", "4"));
Now I need to check that there is not a single item in the drop-down list selected in the drop-down list for which I created the necessary field validator dynamically as follows:
Dim reqPrefGroupValidator As RequiredFieldValidator = New RequiredFieldValidator() reqPrefGroupValidator.ControlToValidate = "objPreferenceDropdownList" reqPrefGroupValidator.InitialValue = "0" reqPrefGroupValidator.SetFocusOnError = True prefdiv.Controls.Add(reqPrefGroupValidator)
The task is required. Validator only works if the drop-down list is empty, if I need to remove the required identifier when the value of the selected dropdownlist element is zero.
source share