How to validate a DropDownList in asp.net


DropDownList Validation

DropDownList is an asp.net 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 asp.net 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 asp.net 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";

            DropDownList1.Items.Add("Select");
            DropDownList1.Items.Add("Html");
            DropDownList1.Items.Add("Css");
            DropDownList1.Items.Add("Java");
            DropDownList1.Items.Add("Asp.Net");
            
        }

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

    }
}

HTML Code

<%@ Page Language="C#" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    
  protected void Button1_Click(object sender, System.EventArgs e) {  
        Label1.Text = "You selected: " +  
            DropDownList1.SelectedItem.Text.ToString();  
    }  
</script>  
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
       <asp:DropDownList ID="DropDownList1" runat="server">  
            <asp:ListItem Selected="True">Select  
            <asp:ListItem>Html</asp:ListItem>  
            <asp:ListItem>Css</asp:ListItem>  
            <asp:ListItem>Java</asp:ListItem>  
            <asp:ListItem>Asp.Net</asp:ListItem>  
        </asp:DropDownList>  
        <asp:RequiredFieldValidator  
             ID="RequiredFieldValidator1"  
             runat="server"  
             ControlToValidate="DropDownList1"  
             InitialValue="Select"  
             ErrorMessage="Select One!"  
             >  
        </asp:RequiredFieldValidator>  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />  
        
    </div>
    </form>
</body>
</html>
Output
How to validate a DropDownList in asp.net
How to validate a DropDownList in asp.net

Share this article on