?
Solved

ListBox not validating with RequiredFieldValidator

Posted on 2013-01-25
5
Medium Priority
?
396 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
  • 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 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