Click here to Skip to main content
14,843,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear sir,

I want to Display three dropwnlist in gridview.
First DropdownList As Country.
Second DropdownList As State.
third DropdownList As District.
How to bind multiple DropDownList in Gridview in asp.net?
Then
How to implement cascading of Dropdownlist in Gridview in asp.net?

Please You will send link or Code.

Thanks
Mahendra Kumar Das
Posted

Well there is a control named:
ASP.NET AJAX Cascading Drop Down Control :) you can implement it easily;
AJAX Cascading Drop Down Control[^]
AJAX Cascading Drop Down with Database[^]
A video tutorial[^]
A similar example that you may want to look[^]
   
v2
Try this Link

Link [^]
   
v2
Use the below code

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

<!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>

    <script type="text/javascript" language="javascript">
        var actionRID = "";
        function BindStates(objThis) {
            var countryID = objThis.options[objThis.selectedIndex].value;
            if (countryID > 0) {
                PageMethods.getStates(countryID, onSuccessgetStates, onErrorgetStates);
                actionRID = objThis.id.split("_")[1].split("ctl")[1];
            }
        }
        function onSuccessgetStates(result, userContext, methodName) {
            var objState = document.getElementById("GridView1_ctl" + actionRID + "_ddlState");
            objState.options.length = 0;
            objState.add(new Option("--Select--", "0"));
            for (var i = 0; i < result.length; i++) {
                objState.add(new Option(result[i].StateName, result[i].StateID));
            }
        }
        function onErrorgetStates() {
            alert("error in retrieving Cities.");
            var objState = document.getElementById("GridView1_ctl" + actionRID + "_ddlState");
            objState.options.length = 0;
        }

        function BindCities(objThis) {
            var CityID = objThis.options[objThis.selectedIndex].value;
            if (CityID > 0) {
                PageMethods.getCities(CityID, onSuccessgetCities, onErrorgetCities);
                actionRID = objThis.id.split("_")[1].split("ctl")[1];
            }
        }
        function onSuccessgetCities(result, userContext, methodName) {
            var objState = document.getElementById("GridView1_ctl" + actionRID + "_ddlCity");
            objState.options.length = 0;
            objState.add(new Option("--Select--", "0"));
            for (var i = 0; i < result.length; i++) {
                objState.add(new Option(result[i].CityName, result[i].CityID));
            }
        }
        function onErrorgetCities() {
            alert("error in retrieving Cities.");
            var objState = document.getElementById("GridView1_ctl" + actionRID + "_ddlCity");
            objState.options.length = 0;
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="ddlCountry" onchange="BindStates(this)">
                            <asp:ListItem Text="--Select--" Value="0">
                            </asp:ListItem>
                            <asp:ListItem Text="INDIA" Value="1">
                            </asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="ddlState" onchange="BindCities(this)">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="ddlCity">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable("tbl1");
        dt.Columns.Add("ID");
        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = "ROW " + i.ToString();
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    [WebMethod]
    public static StateList getStates(Int32 countryID)
    {
        StateList oBOStateList = new StateList();

        if (countryID == 1)
        {
            oBOStateList.Add(new State { StateID = 1, StateName = "New Delhi" });
            oBOStateList.Add(new State { StateID = 2, StateName = "UP" });
            oBOStateList.Add(new State { StateID = 3, StateName = "BIHAR" });
            oBOStateList.Add(new State { StateID = 4, StateName = "West Bengal" });
        }

        return oBOStateList;
    }

    [WebMethod]
    public static CityList getCities(Int32 cityID)
    {
        CityList oBOCityList = new CityList();

        switch (cityID)
        {
            case 1:
                oBOCityList.Add(new City { CityID = 1, CityName = "Lajpat Nagar" });
                oBOCityList.Add(new City { CityID = 2, CityName = "South Ex." });
                oBOCityList.Add(new City { CityID = 3, CityName = "Nehru Place" });
                oBOCityList.Add(new City { CityID = 4, CityName = "Greater Kailash" });
                break;
            case 2:
                oBOCityList.Add(new City { CityID = 1, CityName = "NOIDA" });
                oBOCityList.Add(new City { CityID = 2, CityName = "Ghaziabad" });
                oBOCityList.Add(new City { CityID = 3, CityName = "Kanpur" });
                oBOCityList.Add(new City { CityID = 4, CityName = "Lucknow" });
                break;
            case 3:
                oBOCityList.Add(new City { CityID = 1, CityName = "Patna" });
                oBOCityList.Add(new City { CityID = 2, CityName = "Aurangabad" });
                oBOCityList.Add(new City { CityID = 3, CityName = "Gaya" });
                oBOCityList.Add(new City { CityID = 4, CityName = "Jahanabad" });
                break;
            default:
                break;
        }

        return oBOCityList;
    }
}

public class State
{
    public State()
    { }

    public Int32 StateID
    { get; set; }
    public string StateName
    { get; set; }
}
public class StateList : List<State>
{
    public StateList()
    { }
}

public class City
{
    public City()
    { }

    public Int32 CityID
    { get; set; }
    public string CityName
    { get; set; }
}
public class CityList : List<City>
{
    public CityList()
    { }
}
   
v3
c#
do it through  SqlDataSource
   
Hi Mahendra,

Try this,


First you have to create 3 DropDown into your aspx page and set AutoPostBack="ture" and also it's OnCheckedChanged Event which will call code behind function.

First in Page load you have to bind Country DropDown and on Country DropDown OnCheckedChanged event you have to bind State DropDown and on State DropDown OnCheckedChanged event you have to bind City DropDown .

Like This,

On .Aspx Page :=

xml
<asp:DropDownList ID="ddlCountry" runat="server" Width="100%" AutoPostBack="true"
                                                       CausesValidation="false" 
                                                       OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" CssClass="divisionClass">
                                                   </asp:DropDownList>



xml
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" Width="100%" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
                                                        CausesValidation="false"  CssClass="divisionClass">
                                                    </asp:DropDownList>

xml
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" Width="100%" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
                                                        CausesValidation="false"  CssClass="divisionClass">
                                                    </asp:DropDownList>



On Code Behind Page that is .cs :=

In Page Load Write Below Function :

sql
ddlCountry.DataSource = fn_getCountry()
            ddlCountry.DataTextField = "CountryName"
            ddlCountry.DataValueField = "CountryID"
            ddlCountry.DataBind()
            ddlCountry.Items.Insert(0, New ListItem("Select Country", ""))
            ddlState.Items.Insert(0, New ListItem("Select State", ""))
            ddlCity.Items.Insert(0, New ListItem("Select City", ""))
            ddlState.Enabled = False
            ddlCity.Enabled = False


then

C#
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
<pre lang="sql">ddlState.DataSource = fn_getStateByCountryID(countryID)
                ddlState.DataTextField = "StateName"
                ddlState.DataValueField = "StateID"
                ddlState.DataBind()
                ddlState.Items.Insert(0, New ListItem("Select State", ""))
                ddlCity.SelectedIndex = 0
                ddlState.Enabled = True
                ddlCity.Enabled = False



}


C#
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{

<pre lang="sql">ddlCity.DataSource = fn_getCityByStateID(stateID)
                ddlCity.DataTextField = "CityName"
                ddlCity.DataValueField = "CityID"
                ddlCity.DataBind()
                ddlCity.Items.Insert(0, New ListItem("Select City", ""))
                ddlCity.Enabled = True


}


C#
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{

}



Here,
fn_getCountry is a function which will Execute a query and Holding a list of Country.
fn_getStateByCountryID(countryID) is function which will hold a State list of Selected Country.
and
fn_getCityByStateID(stateID) is function which will hold a City list of Selected State.

All d Best,

I Hope this will help You.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900