Sunday, 31 August 2014

Validate Listbox using Require filed validator control in asp.net

In this article today I will explain how we can Validate Listbox

using Require filed validator control in asp.net

Example:
Add a webform to project. Drag and drop the Listbox control from toolbox to webform.
<center>
    <fieldset style="width:30%;height:100px">
    <legend>Validate Listbox using Requirefield validator control</legend>
       Please Select Country : <asp:ListBox runat="server" ID="lstboxcountry" Width="35%">
        <asp:ListItem Value="0">--Select Country--</asp:ListItem>
       <asp:ListItem Value="1">India</asp:ListItem>
 <asp:ListItem Value="2">China</asp:ListItem>
 <asp:ListItem Value="3">Russia</asp:ListItem>
 <asp:ListItem Value="4">Cuba</asp:ListItem>
 <asp:ListItem Value="5">Iran</asp:ListItem>
 <asp:ListItem Value="6">Brazil</asp:ListItem>
 <asp:ListItem Value="7">Sri lanka</asp:ListItem>
       </asp:ListBox>
       </br>                
                <asp:RequiredFieldValidator ID="rfvlstboxcountry"
            ControlToValidate="lstboxcountry" InitialValue="0" runat="server"
            ErrorMessage="Please select Country" ForeColor="#FF3300"></asp:RequiredFieldValidator>
        </fieldset>
        </center>

Note: If not want “—Select Country—“ option then set the InitialValue="".
Build and run the project.




No comments:

Post a Comment