Link to home
Start Free TrialLog in
Avatar of Lorna70
Lorna70

asked on

ListBox not validating with RequiredFieldValidator

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>
Avatar of YZlat
YZlat
Flag of United States of America image

Try setting InitialValue of your validator to "" instead of "0"
why do you need

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

in your ListBox?

Btw, your code worked for me without an issue
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lorna70
Lorna70

ASKER

Thanks everyone - yes no idea why I added 'Please select' - just removed it and it now works :-)
"everyone"?? I was the only one who replied:)