Skip to main content

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.
DropDownListValidation.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "Your selected item is : " +
            DropDownList1.SelectedItem.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to validate a DropDownList in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            DropDownList Validation
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label1"
            runat="server"
            Font-Bold="true"
            Font-Names="Comic Sans MS"
            ForeColor="ForestGreen"
            Font-Italic="true"
            Font-Size="X-Large"
            />
        <br /><br /><br />
        <asp:DropDownList 
            ID="DropDownList1" 
            runat="server"
            Width="350"    
            Font-Size="X-Large"    
            Font-Names="Comic Sans MS"
            ForeColor="MidnightBlue"
            BackColor="FloralWhite"
            >
            <asp:ListItem Selected="True">Choose One</asp:ListItem>
            <asp:ListItem>BulletedList</asp:ListItem>
            <asp:ListItem>Button</asp:ListItem>
            <asp:ListItem>Calendar</asp:ListItem>
            <asp:ListItem>DataGrid</asp:ListItem>
            <asp:ListItem>DataList</asp:ListItem>
            <asp:ListItem>DataPager</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="DropDownList1"
             InitialValue="Choose One"
             ErrorMessage="* Please select an item."
             ForeColor="Red"
             Font-Names="Impact"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Validate DropDownList" 
            OnClick="Button1_Click"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="DodgerBlue"
            Font-Names="Monaco"
            Height="45"
            Width="350"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples

Popular posts from this blog

How to use Table in asp.net

Table Web Server Control ASP.NET Table web server control display a table on a web page. It is a .NET web server control so we can program it in server code such as add, remove table row, cell programmatically. TableRow and TableCell web server controls allow us to display content for the Table. Table server control display tabular data and we can format Table server control using it's built in methods and properties. The big difference between HTML table and asp.net Table server control is that we can manipulate Table server control using an object model. Generally we should use Table server control when we intend to add remove rows and cells (columns) programmatically at run time.

Table server control acts as a parent (container) for TableRows controls. Table control's Rows property is a collection of TableRow objects. each TableRow control has a collection named Cells. Cells collection contains TableCell objects.

Actually table server control display the TableCell con…

How to use RadioButton control in asp.net c#

RadioButton web server control RadioButton control lets you make a group of radio buttons with other RadioButton. If you set it's GroupName property same for multiple radio buttons then all radio buttons with same name act as a single group. Within a group you can only select one RadioButton at a time. RadioButton group work as like RadioButtonList. With RadioButton control you can more customize it than RadioButtonList items.

Back color, border color, border style, font names, font bold, font italic, fore color (text color) and many more property help you to design RadioButton. It also support CSS class, theme and skin. Even validation are works with RadioButton control. RadioButton have AutoPostBack property and CheckChanged event. By using those feature you can determine immediately which RadioButton is selected from a group.

This example uses a RadioButton group with two RadioButton. Here we assign AutoPostBack property value to true and setup a CheckChanged event. When …

How to use RadioButtonList control in asp.net c#

RadioButtonList web server control ASP.NET RadioButtonList control enable user to select an item from list. RadioButtonList support data bind programmatically from database. We can also populate it manually by input list item inside RadioButtonList tag. RadioButtonList is a single selection radio button group. RadioButtonList have an items collection. We can determine which item is selected by test it's SelectedItem property.

We can change RadioButtonList design by setting up it's various property as like BackColor, BorderColor, BorderStyle, BorderWidth, CellPadding, CellSpacing, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size etc. there are more three properties RepeatColumns, RepeatDirection and RepeatLayout which help you to place list item vertically or horizontally.

RadioButtonList have standard AutoPostBack property. As like other list control RadioButtonList have an excellent event SelectedIndexChanged. So that when someone select an item it…