Solved

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

Posted on 2011-03-02
14
573 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 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