Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validator for drop down list in asp.net

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.

like image 383
Chris Avatar asked Feb 23 '11 09:02

Chris


People also ask

How to validate a Drop down List in ASP net?

To use ASP.NET validator with DropDownList control, set the ID of the DropDownList as the value of the ControlToValidate property of the validator. Executing the above code will validate the DropDownList control values on every form submit before post back occurs as given below.

What is validation summary in asp net?

The ValidationSummary class is used to summarize the error messages from all validators on a Web page in a single location. You can summarize the error messages from a group of validators on a Web page by assigning the ValidationSummary control to a validation group by setting the ValidationGroup property.


1 Answers

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 with ValueToCompare="-- Please Select --" and Operator="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 to Operator="NotEqual" if you wanted to take the approach of using a CompareValidator.

like image 136
Carson63000 Avatar answered Sep 30 '22 09:09

Carson63000