ASP.NET LISTBOX VALIDATION PASS WITHOUT A VALUE SELECTED

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
C# 
C#
 1063  View(s)
Ratings:
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>

  1. ezra heywood

    Post:145

    Points:1019
    Re: asp.net listbox validation pass without a value selected

    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>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!