简体   繁体   中英

how to bind data to the dropdownlist on selected value of the other dropdownlist in asp.net?? I have already bind data to the all the dropdown

//.aspx.cs code:

protected void ddldistrict_SelectedIndexChanged(object sender, EventArgs e)
{
    try {
        ddltaluka.Enabled = true;
        string d1 = ddldistrict.Text;
        NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*******;Database=guj_data;");
        conn.Open();
        string sql = "SELECT tname FROM taluka_geo_bnd_box WHERE district='"+d1+"'";
        NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

        ds.Reset();
        da.Fill(ds);
        dt = ds.Tables[0];
        ddltaluka.DataSource = ds;
        ddltaluka.DataTextField = "tname";
        ddltaluka.DataBind();

        conn.Close();
      }
    catch(Exception e3)
    {
        throw e3;
    }
}
protected void ddltaluka_SelectedIndexChanged(object sender, EventArgs e)
{
    try { 
    ddlvillage.Enabled = true;
    string t1 = ddltaluka.Text;
    string d1 = ddldistrict.Text;
    NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=guj_data;");
    conn.Open();
    string sql = "SELECT vname FROM village_boundary_geo_bnd_box WHERE tname='"+t1+"' AND district='"+d1+"'";
    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

    ds.Reset();
    da.Fill(ds);
    dt = ds.Tables[0];
    ddlvillage.DataSource = ds;
    ddlvillage.DataTextField = "vname";
    ddlvillage.DataBind();

    conn.Close();
  }
    catch(Exception e4)
    {
        throw e4;
    }
}

If I am understanding you correctly; ddlvillage data bind happens at page load when the method ddltaluka_selectedIndexChanged is called, you try to bind new data do it but it goes back to the original ddlVillage list?

If this is the case you need to only do the initial databind for ddlVillage on the initial page load and not each post back

  if (!IsPostBack)
  {
       //bind your initial data here

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM