Home > DeveloperSection > Forums > asp.net listbox validation pass without a value selected
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    December-18-2013 12:52 AM

 C# C# 
Ratings:


 1 Reply(s)
 803  View(s)
Rate this:

I have a listbox with Data coming from a database. But the validation passes even when value is not selected. I have tried ,just simple required field validator and does not work either. Here is the code.

<asp:Panel ID="panelDelivery" runat="server"  Visible="false"

               style="position: relative; top: -130px; background-color: #66FF33; left: 0px;"  >

           <asp:Label ID="lblOverWeight" runat="server" Width="344px"></asp:Label><br />

           <asp:ListBox ID="listBxDelivery" runat="server" DataSourceID="Delivery"

               DataTextField="DataText" DataValueField="Price" Width="489px"        

               AppendDataBoundItems="True" CausesValidation="True" AutoPostBack="True"

                   style="top: 0px; left: 0px">

           </asp:ListBox>

<asp:RequiredFieldValidator ID="rfvDelivery" runat="server"

                   ErrorMessage="* Select Delivery " ControlToValidate="listBxDelivery"></asp:RequiredFieldValidator>



ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    December-18-2013 1:26 AM

Hi Manoj,

By default requiredFieldValidator will compare its initialValue(default is "") and ControlToValidate selected value. make sure you not set the listbox selectedvalue, if you change it also change the initial value of the requiredFieldValidator. See examples below

<asp:ListBox ID="ListBox1" runat="server">                                                                                                       

                            <asp:ListItem Text="1" Value="1" ></asp:ListItem>

                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>

                            <asp:ListItem Text="2" Value="2" ></asp:ListItem>

    </asp:ListBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"    ErrorMessage="Error, Select value" ControlToValidate="ListBox1"></asp:RequiredFieldValidator>

<asp:ListBox ID="ListBox1" runat="server">

                        <asp:ListItem Text="1" Value="1" Selected="True"></asp:ListItem>

                        <asp:ListItem Text="2" Value="2" ></asp:ListItem>

                        <asp:ListItem Text="2" Value="2" ></asp:ListItem>

</asp:ListBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ListBox1" InitialValue="1"></asp:RequiredFieldValidator>


Don't want to miss updates? Please click the below button!

Follow MindStick