简体   繁体   English

如何将数据绑定到asp.net中另一个dropdownlist的选定值上的dropdownlist? 我已经将数据绑定到所有下拉列表

[英]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: //.aspx.cs代码:

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? 当调用方法ddltaluka_selectedIndexChanged时,ddlvillage数据绑定发生在页面加载时,您尝试绑定新数据吗,但是它又回到了原始的ddlVillage列表?

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 如果是这种情况,您只需要在初始页面加载时对ddlVillage进行初始数据绑定,而不必每次回发

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

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net的下拉列表中从数据库中绑定选定的值 - How to bind selected value from database in dropdownlist in asp.net ASP.NET如何在GridView外部的DropDownList中绑定数据? - ASP.NET How to bind data in DropDownList outside GridView? 从ASP.NET DropDownList绑定选定的值 - Bind selected value from ASP.NET DropDownList GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值 - DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c# ASP如何在详细信息视图中绑定下拉列表的选定值 - ASP How to bind the selected value of a dropdownlist in detailsview 无法将数据绑定到gridview asp.net中的dropdownlist - failed to bind data to dropdownlist in gridview asp.net 如何将值绑定到ASP.NET MVC中的dropdownlist? - How to bind value to dropdownlist in asp.net mvc? ASP.NET-如何为数据绑定下拉列表提供占位符值? - ASP.NET - How to have a placeholder value for a data binded dropdownlist? 如何在asp.net中使用jQuery获取下拉列表选择的值绑定? - How to get dropdownlist selected value bind using jquery in asp.net? 如何使用jQuery将JSON数据绑定到Asp.net MVC中的dropdownlist - How to bind JSON data to dropdownlist in Asp.net MVC using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM