How to Get Second Dropdownlist Values Based On First Dropdownlist In Asp.net


How to Get Second Dropdownlist Values Based On First Dropdownlist In Asp.net

In these articles i explained about "How to populate one dropdownlist values based on selection in another dropdownlist in asp.net by using c#."

HTML Markup : Design Page

Adding two dropdownlist to get country and city name

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
     <tr>
      <td>Choose Country First : </td>
      <td>
          <asp:DropDownList ID="ddl_country" runat="server"  Width="150px" 
              AutoPostBack="True" onselectedindexchanged="ddl_country_SelectedIndexChanged">
          </asp:DropDownList>
      </td>
     </tr>
     <tr>
     <td>Choose City  : </td>
      <td>
          <asp:DropDownList ID="ddl_city" runat="server" Width="150px">
          </asp:DropDownList><br />
      </td>
     </tr>
     <tr>
      <td colspan="2">
       <asp:Button ID="btn_display" runat="server" Text="Display" 
              onclick="btn_display_Click" />
      </td>
     </tr>
     <tr>
      <td colspan="2">
       <asp:Label ID="lbl_display" runat="server"></asp:Label>
      </td>
     </tr>
    </table>
    </form>
</body>
</html>

C# Coding

C# Coding : Namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

C# Coding : Page Load

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ddl"].ConnectionString);
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    string query;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCountry();
            ddl_city.Items.Insert(0, " No City Available ");
        }
    }
}

C# Coding : Get Country

GetCountry()

method is used to get all countries data from database and bind into Dropdownlist Control

    private void GetCountry()
    {
        query = "Select * from Country";
        da = new SqlDataAdapter(query, con);
        
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddl_country.DataSource = ds;
            ddl_country.DataTextField = "countryname";
            ddl_country.DataValueField = "countryid";
            ddl_country.DataBind();
            ddl_country.Items.Insert(0, new ListItem(" Choose Country ", "0"));
            ddl_country.SelectedIndex = 0;
        }
    }

C# Coding : Dropdownlist SelectedIndexChanged Event


protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e)
    {
        ds.Clear();
        string get_countryname, countryname;
        countryname = ddl_country.SelectedItem.Text;
        get_countryname = ddl_country.SelectedValue.ToString();

        if (get_countryname != "0")
        {
            query = "Select cityid, cityname from City where countryid='" + get_countryname.ToString() + "'";
            da = new SqlDataAdapter(query, con);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddl_city.DataSource = ds;
                ddl_city.DataTextField = "cityname";
                ddl_city.DataValueField = "cityid";
                ddl_city.DataBind();
                ddl_city.Items.Insert(0, new ListItem("Choose " + countryname.ToString() + " City ", "0"));
                ddl_city.SelectedIndex = 0;
            }
        }
        else
        {
            ddl_city.Items.Insert(0, " No City Available ");
            ddl_city.DataBind();
        }
        
    }

C# Coding : Button Click Event


    protected void btn_display_Click(object sender, EventArgs e)
    {
        string display;
        display = " Your Country : " + ddl_country.SelectedItem.Text + "
Your City : " + ddl_city.SelectedItem.Text + ""; lbl_display.Text = display; } }
0 Komentar untuk "How to Get Second Dropdownlist Values Based On First Dropdownlist In Asp.net"

Back To Top