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 & Interests:<br />
<asp:ListBox ID="ddlInterests" runat="server" SelectionMode="Multiple"
DataSourceID="SqlDataSourc e3" DataTextField="Interests_D esc"
DataValueField="Interests_ ID" AppendDataBoundItems="True ">
<asp:ListItem Value="0">-- Please select --</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidato r ID="rfvInterests" runat="server" Display="Dynamic" InitialValue="0"
ControlToValidate="ddlInte rests" ErrorMessage="Please select at least one hobby/interest"></asp:Requ iredFieldV alidator>
</div>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:Connecti onString %>"
SelectCommand="spGetIntere sts" SelectCommandType="StoredP rocedure">
</asp:SqlDataSource>
</td>
<tr>
<td align="right">
<asp:Button class="btnDefault" ID="btnSubmit" runat="server" CausesValidation="True" onclick="searchSkillset" Text="Search" />
</td> </tr>
</table>
<table>
<tr>
<td>
Hobbies & Interests:<br />
<asp:ListBox ID="ddlInterests" runat="server" SelectionMode="Multiple"
DataSourceID="SqlDataSourc
DataValueField="Interests_
<asp:ListItem Value="0">-- Please select --</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidato
ControlToValidate="ddlInte
</div>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:Connecti
SelectCommand="spGetIntere
</asp:SqlDataSource>
</td>
<tr>
<td align="right">
<asp:Button class="btnDefault" ID="btnSubmit" runat="server" CausesValidation="True" onclick="searchSkillset" Text="Search" />
</td> </tr>
</table>
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
<asp:ListItem Value="0">-- Please select --</asp:ListItem>
in your ListBox?
Btw, your code worked for me without an issue
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:)