Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-02
14
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Industry Leaders: 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

730 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