Solved

ListBox not validating with RequiredFieldValidator

Posted on 2013-01-25
5
388 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 250 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

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

Suggested Solutions

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 …
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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