Descending DropDown List In ASP.NET

Introduction

A Dropdown list is a combination box that contains a list of multiple items. The user may select an item from the menu by dragging the mouse from the menu title or by clicking the title and then clicking the item. The dropdown list box is read-only. A drop-down list is a list in which the selected item is always visible, and the others are visible on demand by clicking a drop-down button.

In this article I am describing a descending dropdown list in ASP.NET.

Complete Program

DropDownDemo.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. public partial class DropDownDemo : System.Web.UI.Page  
  10. {  
  11.     private string strconnection = "Data Source=MCNDESKTOP27;Initial Catalog=MysimpleDB;Persist Security Info=True;User ID=sa;Password=**********";  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!IsPostBack)  
  15.         {  
  16.             Binddropdown();  
  17.         }  
  18.     }  
  19.     protected void Binddropdown()  
  20.     {  
  21.         SqlConnection strcon = new SqlConnection(strconnection);  
  22.         strcon.Open();  
  23.         SqlCommand cmmd = new SqlCommand("select * from CountryTable", strcon);  
  24.         SqlDataAdapter da = new SqlDataAdapter(cmmd);  
  25.         DataSet ds = new DataSet();  
  26.         da.Fill(ds);  
  27.         strcon.Close();  
  28.         ddlCountry.DataSource = ds;  
  29.         ddlCountry.DataTextField = "CountryName";  
  30.         ddlCountry.DataValueField = "CountryId";  
  31.         ddlCountry.DataBind();  
  32.         ddlCountry.Items.Insert(0,new ListItem("--select--","0"));  
  33.         ddlState.Items.Insert(0, new ListItem("--select--""0"));  
  34.         ddlRegion.Items.Insert(0, new ListItem("--select--""0"));  
  35.     }  
  36.     protected void ddlCountry_IndexChanged(object sender, EventArgs e)  
  37.     {  
  38.         int CountryId = Convert.ToInt32(ddlCountry.SelectedValue);  
  39.         SqlConnection strcon = new SqlConnection(strconnection);  
  40.         strcon.Open();  
  41.         SqlCommand cmmd = new SqlCommand("select * from StateTable where CountryId=" + CountryId, strcon);  
  42.         SqlDataAdapter da = new SqlDataAdapter(cmmd);  
  43.         DataSet ds = new DataSet();  
  44.         da.Fill(ds);  
  45.         strcon.Close();  
  46.         ddlState.DataSource = ds;  
  47.         ddlState.DataTextField = "StateName";  
  48.         ddlState.DataValueField = "StateId";  
  49.         ddlState.DataBind();  
  50.         ddlState.Items.Insert(0, new ListItem("--select--""0"));  
  51.         if (ddlState.SelectedValue == "0")  
  52.         {  
  53.             ddlRegion.Items.Clear();  
  54.             ddlRegion.Items.Insert(0, new ListItem("--select--""0"));  
  55.         }  
  56.     }  
  57.     protected void ddlState_IndexChanged(object sender, EventArgs e)  
  58.     {  
  59.         int StateId = Convert.ToInt32(ddlState.SelectedValue);  
  60.         SqlConnection strcon = new SqlConnection(strconnection);  
  61.         SqlCommand cmmd = new SqlCommand("select * from RegionTable where StateId=" + StateId, strcon);  
  62.         SqlDataAdapter da = new SqlDataAdapter(cmmd);  
  63.         DataSet ds = new DataSet();  
  64.         da.Fill(ds);  
  65.         strcon.Close();  
  66.         ddlRegion.DataSource = ds;  
  67.         ddlRegion.DataTextField = "RegionName";  
  68.         ddlRegion.DataValueField = "RegionId";  
  69.         ddlRegion.DataBind();  
  70.         ddlRegion.Items.Insert(0,new ListItem("--select--","0"));  
  71.     }  
  72. }   

DropDownDemo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownDemo.aspx.cs" Inherits="DropDownDemo" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <style type="text/css">  
  7.         .auto-style1  
  8.         {  
  9.             font-size: large;  
  10.         }  
  11.     </style>  
  12. </head>  
  13. <body>  
  14.     <form id="form1" runat="server">  
  15.     <div>  
  16.       <strong>Descending Dropdown Sample</strong>  
  17.         <br />  
  18.         <br />  
  19.         <asp:Label ID="Label1" runat="server" Text="Select Country" style="z-index: 1; left: 10px; top: 73px; position: absolute"></asp:Label>  
  20.     
  21.         <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_IndexChanged"  
  22.             style="z-index: 1; left: 138px; top: 65px; position: absolute">  
  23.         </asp:DropDownList>  
  24.         <br />   
  25.     </div>  
  26.         <asp:Label ID="Label2" runat="server" Text="Select State" style="z-index: 1; left: 9px; top: 105px; position: absolute"></asp:Label>  
  27.          
  28.         <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlState_IndexChanged"  
  29.             style="z-index: 1; left: 139px; top: 102px; position: absolute">  
  30.         </asp:DropDownList>  
  31.         <br />  
  32.         <asp:Label ID="Label3" runat="server" Text="Select Region" style="z-index: 1; left: 6px; top: 140px; position: absolute"></asp:Label>  
  33.    <br />
  34.         <asp:DropDownList ID="ddlRegion" runat="server" style="z-index: 1; left: 139px; top: 140px; position: absolute">  
  35.         </asp:DropDownList>  
  36.     </form>  
  37. </body>  
  38. </html>   

Output


Animation1.gif

For more information, download the attached sample application.


Similar Articles