Validating a ListBox Using Required Field Validator in Asp.Net C# VB.Net

When we bind data into a ListBox Control we need to mention one or more items as required. To do that you have to add a Required Field Validator & mention the InitialValue property =””. The example is given below:

Objective/Outcome:
Validating a ListBox Using Required Field Validator in Asp.Net C# VB.Net

Using Required Field Validator for ListBox Control:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID = "ListBox1" runat = "server" Width="200" Height="200" SelectionMode = "Multiple">
        <asp:ListItem Value="1">January</asp:ListItem>
        <asp:ListItem Value="2">February</asp:ListItem>
        <asp:ListItem Value="3">March</asp:ListItem>
        <asp:ListItem Value="4">April</asp:ListItem>
        <asp:ListItem Value="5">May</asp:ListItem>
        <asp:ListItem Value="6">June</asp:ListItem>
        <asp:ListItem Value="7">July</asp:ListItem>
        <asp:ListItem Value="8">August</asp:ListItem>
        <asp:ListItem Value="9">September</asp:ListItem>
        <asp:ListItem Value="10">October</asp:ListItem>
        <asp:ListItem Value="11">November</asp:ListItem>
        <asp:ListItem Value="12">December</asp:ListItem>
    </asp:ListBox> 

    <asp:RequiredFieldValidator ID = "RequiredFieldValidator1" ControlToValidate ="ListBox1" InitialValue = ""  runat="server" ErrorMessage = "*" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <hr />
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, ListBox, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*