Solved

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

Posted on 2011-03-02
14
550 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

8 Experts available now in Live!

Get 1:1 Help Now