Thursday, 26 July 2012

Populating Casading Dropdownlists in asp.net

In This post we are going to  Populating cascading drop down list from database.


here is the sample code in our aspx page:




<table>
    <tr>
        <td>First</td>
        <td><asp:DropDownList ID="drpCountry" runat="server"  AutoPostBack="true"
                onselectedindexchanged=" drpCountry _SelectedIndexChanged"></asp:DropDownList></td>
    </tr>
    <tr>
        <td>Secord</td>
        <td><asp:DropDownList ID="drpState" runat="server" AutoPostBack="true"
                onselectedindexchanged=" drpState _SelectedIndexChanged"> 
            </asp:DropDownList></td>
    </tr>
    <tr>
        <td>Thrid</td>
        <td><asp:DropDownList ID="drpCity" runat="server"></asp:DropDownList></td>
    </tr>
</table>

Now in Code behind page bind first dropdownlist on Page_Load event.

protected void Page_Load(object sender, EventArgs e)
 {
   if (!Page.IsPostBack)
   {
     // bind the first dropdown here.
   }
 }

protected void  drpCountry _SelectedIndexChanged (object sender, EventArgs e)
{
            SqlCommand cmd = new SqlCommand(select * from state where       country_ID='"+drpCountry.SelectedValue.ToString()+"', con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
              drpState .Items.Clear();
                ListItem li = new ListItem();
                li.Text = "---Select---";
                li.Value = "0";
                drpState .Items.Add(li);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        ListItem li1 = new ListItem();
                        li1.Text = ds.Tables[0].Rows[i]["State_Name"].ToString();
                        li1.Value = ds.Tables[0].Rows[i][" State_ID "].ToString();
                        drpState.Items.Add(li1);
                    }
            }
}

protected void   drpState _SelectedIndexChanged  (object sender, EventArgs e)
{
            SqlCommand cmd = new SqlCommand(select * from City where       country_ID='"+ drpState .SelectedValue.ToString()+"', con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
               drpCity  .Items.Clear();
                ListItem li = new ListItem();
                li.Text = "---Select---";
                li.Value = "0";
                drpCity  .Items.Add(li);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        ListItem li1 = new ListItem();
                        li1.Text = ds.Tables[0].Rows[i]["City_Name"].ToString();
                        li1.Value = ds.Tables[0].Rows[i][" City_ID "].ToString();
                        drpCity .Items.Add(li1);
                    }
            }
}




2 comments:

  1. This is a nice example, I speculate if any user wants to update the row and the grid view, what should do? Is it possible for you to explain or show the code? Thanks!

    DOT NET Development Company | Dot NET Experts

    ReplyDelete