Search code examples
asp.netvisual-studio-2010drop-down-menurequiredfieldvalidatorcomparevalidator

Dropdownlist and field validation is causing a riot


For some reason, I can't get the validator to raise a flag when I do things wrong.

<asp:DropDownList ID="ddlTypeList" runat="server" DataSourceID="ods_TypeOptions" DataTextField="name" DataValueField="id" SelectedValue='<%# Bind("Type") %>' AppendDataBoundItems="true">
<asp:ListItem Text="-" Value="-1" Selected="True"></asp:ListItem> </asp:DropDownList>

The drop down list has nice values, incl the initial dummy.

Neither

<asp:RequiredFieldValidator ID="rfw" runat="server" ControlToValidate="ddlTypeList" InitialValue="-1" ToolTip="Required">*</asp:RequiredFieldValidator>

Nor

<asp:CompareValidator ID="cv" runat="server" ControlToValidate="ddlTypeList" ValueToCompare="-1" Operator="NotEqual" ToolTip="Required">*</asp:CompareValidator>

Raises any flags to say "hey - u messed up, go fix it". For all the google, searching, reviews, swinging big hammers, I have yet to spot what I am doing wrong.

I just want one solution to fix them all.

Oh yes, I also had a ValidationGroup="myGroup" between the DDL, RFV/CV and the button. No luck.


Solution

  • Your code is correct. You must have some element of code or markup that is obstructing the functionality of the validator. Is it possibly that the "-" item has its value changed from "-1" to something else?

    Verify that your button that submits the form has CausesValidation=True.Also, make sure that the parent of the validators is not set to Visible=False or the children will not render on the page.

    Verify that the RequiredFieldValidator and CompareValidator are rendering in the markup by searching for "_cv" or possibly "cv" in the outputted markup. If it is not there, then one of the parent elements is not being rendered or the validators are being deleted.