How to validate a DropDownList in

DropDownList Validation

DropDownList is an list web server control that allow us to select a single item from a drop-down-list at a time. dropdownlist control contains ListItem objects. the following c# example code demonstrate us how can we validate a dropdownlist control in a web form.

When users submit a form without changing selection of a dropdownlist items, then the first item is post to server as selected item if there is no initial selected item. normally web developers put an instruction only item at dropdownlist items top such as an item with text 'select an item' etc. so web developers need to ensure that users select an item from dropdownlist control other than the first item. we can validate a dropdownlist server control using RequiredFieldValidator control.

Requiredfieldvalidator is an validation server control that makes the associated input control as a required field. so we can make the dropdownlist input control as a required field by attaching it with a requiredfieldvalidator control.

To do this we need to set the requiredfieldvalidator ControlToValidate property value to DropDownList control's ID which dropdownlist control we want to validate. requiredfieldvalidator control's InitialValue property get or set the initial value of the associated input control. in this example code dropdownlist initial value is 'Choose One', because we set this text for the dropdownlist control's first item Text property value. initial value is only used for information only, users need to select any one item from dropdownlist control other than it.

Requiredfieldvalidator ErrorMessage property get or set the text for the error message displayed in a ValidationSummary control or the requiredfieldvalidator control itself when validation fail.

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
    public partial class blankpage : System.Web.UI.Page

      protected void Page_Load(object sender, EventArgs e)
            Label1.Visible = false;

            Button1.Text = "Send";

            RequiredFieldValidator1.ErrorMessage = "Select One!";
            RequiredFieldValidator1.ForeColor = System.Drawing.Color.Red;
            RequiredFieldValidator1.ControlToValidate = DropDownList1.ClientID;

            RequiredFieldValidator1.InitialValue = "Select";


        protected void Button1_Click(object sender, EventArgs e)
            Label1.Visible = true;
            Label1.Text = "You selected: <b>" + DropDownList1.SelectedItem.Text.ToString()+"</b>"; 



<%@ Page Language="C#" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
<script runat="server">  
  protected void Button1_Click(object sender, System.EventArgs e) {  
        Label1.Text = "You selected: " +  
<html xmlns="">
<head runat="server">
    <title> Button Control </title>
    <form id="form1" runat="server">
       <asp:DropDownList ID="DropDownList1" runat="server">  
            <asp:ListItem Selected="True">Select  
             ErrorMessage="Select One!"  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />  
How to validate a DropDownList in
How to validate a DropDownList in

Share this article on