Solved

ListBox not validating with RequiredFieldValidator

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web site error 3 40
How to close an asp.net web form using C# ? 4 30
ck editor is taking too much time to be loaded in asp 1 17
VB.NET 2008 - SQL Timeout 9 23
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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