listbox validation pass without a value selected

Total Post:154


 1453  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:RequiredFieldValidator ID="rfvDelivery" runat="server"
                   ErrorMessage="* Select Delivery " ControlToValidate="listBxDelivery"></asp:RequiredFieldValidator>
  1. Re: 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: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:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ListBox1" InitialValue="1"></asp:RequiredFieldValidator>

      Modified On Apr-04-2018 12:19:43 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.