We help IT Professionals succeed at work.

Validate DropDownList ASP.NET

jawhitmoyer
jawhitmoyer asked
on
556 Views
Last Modified: 2012-06-22
I cannot make the validation work on a drop down list, and the other posting I have found offer suggetions that I cannot get to work. I am new to ASP.NET, so maybe I am missing something totally obvious.

Here's the code to populate the DDL:

DBCommand = New OleDbDataAdapter("Select ConsultantID, Name from user_consultants Order By name", DBConn)
DBCommand.Fill(DSconsultants, "consultants")
ddlConsultants.DataSource = DSconsultants.Tables("consultants").DefaultView
ddlConsultants.DataBind()
ddlConsultants.Items.Insert(0, new ListItem("Please Select", 0))

This grabs the data and then adds a "Please Select" line. This part works great. Here's my DDL and validation code:

<asp:dropdownlist
    id="ddlConsultants"
    font-size=8
    runat=server
    DataTextField="Name"
    DataValueField="consultantID">
</asp:dropdownlist>
<asp:comparevalidator
      id="cvConsultants"
      controltovalidate="ddlConsultants"
      Operator="EQUAL"
      value="0"
      display="dynamic"
      runat=server
      text="Please select the Action Item owner."
/>

As soon as I select ANY item in the list the validation fires the error message.

I have wasted enough time fighting it, so I am leaving it to the experts.

Jim
Comment
Watch Question

Commented:
It will give the error if the values of the dropdown do not equal 0. It compares every value to 0 and if they are not the same it will give the error.

Author

Commented:
So what do I need to change?
Top Expert 2004

Commented:
Change to:

<asp:comparevalidator
     id="cvConsultants"
     controltovalidate="ddlConsultants"
     Operator="EQUAL"
     value="Please Select"
     display="dynamic"
     runat=server
     text="Please select the Action Item owner."
/>

Commented:
Do you want to check if something is selected if so it is easier to use a requiredfieldvalidator.

Author

Commented:
I pasted in that code and as soon as I select a name from the list the error message fires.

Author

Commented:
If the user doesn't make a select (leave the Please Select as the value) I want to fire the warning to make a valid selection.

Commented:
What exactly are you comparing, are you looking to see if something is selected or are you comparing against a specific item?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Well, I guess I don't know what I am comparing. I want to make sure the user changed the value in the DDL. That's why I added the "Please Select" item. If there's a better way I am open. I would think this is a common thing to do...otherwise the first person in the list will get a lot of work if the users are stupid/lazy and don't make a valid choice.
Top Expert 2004

Commented:
Didn't even see it was not a field validator:

<asp:requiredfieldvalidator id="cvConsultants" runat="server" ErrorMessage="*" ControlToValidate="ddlConsultants"></asp:requiredfieldvalidator>

Leave value for Please Select blank...

Commented:
A required field validator is the way to go, it checks to see if the selected value is not nothing, which is why you leave the value of the first item blank and just give it text.
Top Expert 2004

Commented:
Guess we're double posting peter...

Commented:
Yup been doing it a lot today, even did it in the same minute as someone else earlier.

Author

Commented:
Thanks Peter. You got the points!

For anyone who cares, here's the final solution:

Set the value in the DDL to Empty:
ddlConsultants.Items.Insert(0, new ListItem("Please Select", ""))

Then use the RequiredFieldValidator:
<asp:RequiredFieldValidator
id="rfvConsultants"
runat="server"
ControlToValidate="ddlConsultants"
Text="Please select the Action Item owner"
display="Dynamic">
</asp:RequiredFieldValidator>

I love Experts-Exchange!

Jim
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.