Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ListBox not validating with RequiredFieldValidator

Posted on 2013-01-25
5
Medium Priority
?
394 Views
Last Modified: 2013-01-25
Why is the following code not validating??  I thought if I set InitialValue="0" then it would throw an error if 'Please Select' or none of the items are selected?? Is there something I need to add??

<table>
<tr>
<td>
    Hobbies &amp; Interests:<br />
<asp:ListBox ID="ddlInterests" runat="server" SelectionMode="Multiple"
        DataSourceID="SqlDataSource3" DataTextField="Interests_Desc"
        DataValueField="Interests_ID" AppendDataBoundItems="True">
        <asp:ListItem Value="0">-- Please select --</asp:ListItem>
    </asp:ListBox>

<asp:RequiredFieldValidator ID="rfvInterests" runat="server" Display="Dynamic" InitialValue="0"
        ControlToValidate="ddlInterests" ErrorMessage="Please select at least one hobby/interest"></asp:RequiredFieldValidator>
    </div>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="spGetInterests" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>
</td>
<tr>
<td align="right">
<asp:Button class="btnDefault" ID="btnSubmit" runat="server"  CausesValidation="True" onclick="searchSkillset" Text="Search" />
</td>        </tr>
    </table>
0
Comment
Question by:Lorna70
[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
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 38818607
Try setting InitialValue of your validator to "" instead of "0"
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38818643
why do you need

  <asp:ListItem Value="0">-- Please select --</asp:ListItem>

in your ListBox?

Btw, your code worked for me without an issue
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1000 total points
ID: 38818668
I think you should try this:

<table>
<tr>
<td>
    Hobbies &amp; Interests:<br />
<asp:ListBox ID="ddlInterests" runat="server" SelectionMode="Multiple" 
        DataSourceID="SqlDataSource3" DataTextField="Interests_Desc" 
        DataValueField="Interests_ID" AppendDataBoundItems="True">
    </asp:ListBox>

<asp:RequiredFieldValidator ID="rfvInterests" runat="server" Display="Dynamic" InitialValue="" 
        ControlToValidate="ddlInterests" ErrorMessage="Please select at least one hobby/interest"></asp:RequiredFieldValidator> 

    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="spGetInterests" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>
</td>
<tr>
<td align="right">
<asp:Button class="btnDefault" ID="btnSubmit" runat="server"  CausesValidation="True" onclick="searchSkillset" Text="Search" />
</td>        </tr>
    </table> 

Open in new window

0
 

Author Comment

by:Lorna70
ID: 38819053
Thanks everyone - yes no idea why I added 'Please select' - just removed it and it now works :-)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38819261
"everyone"?? I was the only one who replied:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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