Solved

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

Posted on 2011-03-02
14
559 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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