Solved

How to set required filed validator to drop down list in ASP.Net?

Posted on 2011-03-02
14
541 Views
Last Modified: 2012-05-11
I am trying to use required field validator for the drop down list and it is not working.
It is working fine with the text boxes.
 
<asp:DropDownList ID="ddlSelect" runat="server" Width="155px" AutoPostBack="True" CausesValidation="True"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlSelect" ErrorMessage="Please select." 				ForeColor="Red" InitialValue=" ">*</asp:RequiredFieldValidator>

Open in new window

0
Comment
Question by:reach_prgmmer
  • 4
  • 4
  • 4
  • +1
14 Comments
 
LVL 8

Expert Comment

by:sriggumma
ID: 35018470
How did you determine that it is not working?

FYI - I just placed this part of given code in a aspx page and placed a asp:button and found it working good.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35018607
How are you populating your dropdown listbox ?

You really need to have a static listitem added to it with a value of "" like :

<asp:DropDownList ID="ddlSelect" runat="server" Width="155px" AutoPostBack="True" CausesValidation="True">
    <asp:ListItem Text="Please select " Value=""/>
</asp:DropDownList>

If you are populating the dropdownlist from the database you also need to set the AppendDataBoundItems attribute to true :
<asp:DropDownList ID="ddlSelect" runat="server" Width="155px" AutoPostBack="True" CausesValidation="True" AppendDataBoundItems="true">
0
 

Author Comment

by:reach_prgmmer
ID: 35019053
sriggumma: I know that it is not working because i am able to perform my button click action. Even when I do not select anything.
jacko72:I tried
<asp:DropDownList ID="ddlSelect" runat="server" Width="155px" AutoPostBack="True" CausesValidation="True" AppendDataBoundItems="true">
and it did not work.
I am still able to perform my button click.
I am populating my dropdown list from the dataset.
ddlSelect.DataSource = ds
            ddlSelect.DataValueField = "ID"
            ddlSelect.DataTextField = "SELECT"
            ddlSelect.DataBind()
0
 
LVL 8

Expert Comment

by:sriggumma
ID: 35019062
Yes.
There should be one ListItem (static or dynamically populated) with a value matching InitialValue of the RequiredFieldValidator. Most of the times the default InitialValue is "" (empty).

0
 
LVL 8

Expert Comment

by:sriggumma
ID: 35019089
So - make sure that either the dataset contains an item matching the InitialValue of RequiredFieldValidator.

or

add a listitem with the InitialValue after all items in DropDownList are populated.

Hope this helps.
0
 

Author Comment

by:reach_prgmmer
ID: 35019105
Below is my dataset
ID   SELECT
1          
2      value1
3      value2
.
.
.

by default when the drop down is loaded it is loaded the ID-1 as the initial value which is a space not a empty string.
I could like to apply the validation whe ID=1
How do I do it?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35019109
Did you add the static list item?
0
 
LVL 8

Accepted Solution

by:
sriggumma earned 250 total points
ID: 35019120
Change the InitialValue of RequiredFieldValidator to 1, instead of " "
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35019143
Change the initial value of the requiredfieldvalidator to 1 then
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 35019488
The problem that you are experiencing is most likely due to the fact that the Required Field validator checks to see if the validated field is NULL, and with your DDL, it is not NULL, it is "" (a blank entry), until your user chooses something.  try chaging the Required Field validator to a Range Validator, and set the range type to "Integer", and the MinValue to 2, and the MaxValue to whatever is the maximum value that the maximum value is in the data being presented.  That way, if the user does not make a selection (so the DDL value remains 1), the validator will detect that and display the necessary message, and not allow the button to be clicked.  This accomplishes the necessary functionality, even though not using the "RequiredField" validator to do it.

AW
0
 

Author Comment

by:reach_prgmmer
ID: 35019502
I am able to validate it now, but the error message is not displayed in the validation summary.
should I change anything in the validation summary?
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 35019579
You don't seem to have specified a validationgroup on your requiredfieldvalidator for the error message to show in the summary you need to specify the same validation group specified in the summary like :

<asp:Validationsummary id="myValidationSummary runat="server" ShowMessageBox="false" ShowSummary="True" ValidationGroup="myValidationGroup"/>

<asp:DropDownList ID="ddlSelect" runat="server" Width="155px" AutoPostBack="True" CausesValidation="True"></asp:DropDownList>  
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlSelect" ErrorMessage="Please select."                             ForeColor="Red" InitialValue="1" ValidationGroup="myValidationGroup">*</asp:RequiredFieldValidator>
0
 

Author Closing Comment

by:reach_prgmmer
ID: 35019602
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now