Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I have a dropdownlist which datasource is filled from DB and with an initial value as "--Select--","0"
If I don't select dropdown it shows "--Select--" and assumes that is entered and so it doesn't ask to enter required field.

Here is my asp.net code

XML
<asp:DropDownList ID="ddlDState" runat="server" Width="100%" AutoPostBack="true">
                       </asp:DropDownList>
                         <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="ddlDState" InitialValue="0" ValidationGroup="Required" ErrorMessage="*" ForeColor="Red"/>


Here is my Vb.Net code
VB
Dim ds As New DataSet
        ds = dbnet.FillStates()
        ddlDState.DataSource = ds
        ddlDState.DataValueField = "STATE"
        ddlDState.DataBind()
        ddlDState.Items.Insert(0, New ListItem("--Select--", "0"))



Now my problem is I want to show if ddl is not selected and is still having "--Select--" it should say "Please enter fields required"

It may be something silly I am missing. But I am not sure of it.
Will anything work with Compare Validator ?
Posted
Updated 5-Feb-15 5:01am
v3
Comments
ZurdoDev 5-Feb-15 11:07am    
I'd suggest just adding a Validat() function in JS and check it there.
sudevsu 5-Feb-15 11:16am    
Meaning you want to me to add Js function on dropdown list and check if it is not "--Select--" then go thru.
I don't think u can javascript function on Dropdown list. Correct me if I am wrong
ZurdoDev 5-Feb-15 11:22am    
Sure. Javascript can access anything on the page.

And if you use jQuery it's very easy:
$("#myselect").val();
sudevsu 5-Feb-15 11:27am    
I am still not clear. You want me add a JSfunction on Button click or ddl change ?
ZurdoDev 5-Feb-15 12:03pm    
You can do it however you want. If you want it to happen immediately then put it on the ddl change. If you want to wait until the form is submitted use OnClientClick of the ASP.Net button used to submit the form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900