How to use RequiredFieldValidator on ListBox in ASP.NET

If you want to apply RequiredField Validator to ListBox control then you should drop Required FieldValidator on design window. In previous example how to use RequiredFieldValidator control.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = "you are selected <br/>" + ListBox1.SelectedItem.Text;

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ListBox ID="ListBox1" runat="server" Height="136px" Width="110px">
            <asp:ListItem>Apple </asp:ListItem>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ListBox1" ForeColor="#FF3300">Select any one in given list</asp:RequiredFieldValidator>
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />

How to use RequiredFieldValidator on ListBox in ASP.NET


Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites