asp.net - How to count items in a DropDownList

Count DropDownList Items
dropdownlist is an asp.net list web server control. all list items contains in a item collection. we can count the number of items in a dropdownlist using this collection's count property (ListItemCollection.Count property as DropDownList.Items.Count). Count property gets the number of list item objects in the collection.

the following asp.net c# example source code demonstrate us how can we count items in a dropdownlist server control programmatically at run time.
DropDownListItemCount.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        int totalItems = DropDownList1.Items.Count;
        Label1.Text = "Total Items: " + totalItems.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get total number of list item in DropDownList, count item</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">DropDownList: Items Count</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="HotPink"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="SaddleBrown"
             Text="asp.net controls"
             >
        </asp:Label>
        <asp:DropDownList 
             ID="DropDownList1"
             runat="server"
             >
             <asp:ListItem>RadioButton</asp:ListItem>
             <asp:ListItem>LoginView</asp:ListItem>
             <asp:ListItem>RadioButtonList</asp:ListItem>
             <asp:ListItem>CreateUserWizard</asp:ListItem>
             <asp:ListItem>Panel</asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <asp:Button 
             ID="Button1"
             runat="server"
             Font-Bold="true"
             ForeColor="SaddleBrown"
             Text="Count Items"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples