I have a drop down list which I am loading from the server side.
<asp:DropDownList ID="ddlOne" runat="server" CssClass="dropDrownClass" Width="80%">
In server side, after loading the drop down I am adding
— Please Select —
I want to make sure that if that is selected than I would display the error message. For that I have written
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ddlOne" ValueToCompare="-- Please Select --" Operator="Equal" Type="String" ErrorMessage="CompareValidator"></asp:CompareValidator>
<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" TargetControlID="CompareValidator1" runat="server">
</asp:ValidatorCalloutExtender>
But it is showing me the error message whenever I am selecting anything in the drop down list.
and when I changed the validator to
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ddlOne" ValueToCompare="0" Operator="Equal" Type="Integer" ErrorMessage="CompareValidator"></asp:CompareValidator>
I am getting the error message on every selection, except the first which is — Please Select — .
Please let me know how to validate the fist item of dropdown list
ISSUE 2
I am getting dual message, one under the dropdown list [which is showing error "Carson63000" in red] and one as a pop up [validator call out]. Same message.
I want that only validator callout should display the message.
Best Answer
Your validator will compare the value of the dropdown's selected item, not the text. The easiest way is often to have an empty string for the value of the "Please Select" item, have a non-empty value for the other items, and then just use a
RequiredFieldValidator
.Additionally, a
CompareValidator
withValueToCompare="-- Please Select --"
andOperator="Equal"
means: check the value of the dropdown, and validate that it is equal to "-- Please Select --"; if not, display the error. Which is the exact opposite of what you need - you'd want to change the operator toOperator="NotEqual"
if you wanted to take the approach of using aCompareValidator
.