如何在Asp.Net C#中填充依赖的下拉列表

时间:2013-01-02 09:55:32

标签: c# asp.net drop-down-menu

我想填充两个下拉列表,根据第一个下拉列表的选择,第二个下拉列表将被填充。

示例:

就像我有两个下拉列表,即 1.ddlCountry 2.ddlState

现在,当选择国家/地区时,根据所选的国家/地区,与该国家/地区相关的将填入州下拉列表中。我希望通过使用C#编码语言重新加载Asp.Net中的整个页面来实现这一目标。

我怎样才能达到同样的目标?

Dropdownlist通过执行查询从数据库中获取数据。

3 个答案:

答案 0 :(得分:0)

  1. 您可以AJAX toolkit CascadingDropDown使用Naresh。 或者
  2. 使用ajax update panel并保留所有Dropdowns。因此,整个页面不会在更改下拉列表值时加载。
  3.   

    你没有给代码提供进一步的解决方案。

答案 1 :(得分:0)

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
  FillStateByCountry();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
  FillLocationByCountryandState();
}

private void FillStateByCountry()
{
    DataSet dstFillState;
    int CountryId = Convert.ToInt32(ddlCountry.SelectedValue.ToString());
    dstFillState = Tbl_State.FillDDLState(CountryId);
    ddlState.DataSource = dstFillState;
    ddlState.DataTextField = "State";
    ddlState.DataValueField = "Id";
    ddlState.DataBind();
}

同样FillLocationByCountryandState();

答案 2 :(得分:0)

在表单中添加两个下拉列表,并将其命名为cmbStatescmbCities

cmbStates(dropwdownlist)选择州名时,cmbCities(dropdownlist)根据州名(cmbStates)生成城市

从数据库中提取数据

  private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=pbs-server;database=p2p;user id=shekar;password=sekhar@1346");
        SqlCommand cmd = new SqlCommand("select states from Country", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.Fill(ds, "Country");
        cmbStates.DataSource = ds.Tables[0];
        cmbStates.SelectedValue = 0;
        con.Close();

    }

    private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=xxxx;database=xxxx;user id=xxxxr;password=xxxxxx");
        SqlCommand cmd = new SqlCommand("select cities from States where cityname = 'cmbStates.SelectedItem.ToString()'", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.Fill(ds, "States");
        cmbCities.DataSource = ds.Tables[0];
        cmbCities.SelectedValue = 0;
        con.Close();           

    }

手动添加项

namespace DropDownlist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbStates.Items.Add("Andhra Pradesh");
            cmbStates.Items.Add("Tamilnadu");
            cmbStates.Items.Add("Karnataka");
            cmbStates.SelectedValue = 0;
        }

        private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbStates.SelectedItem.ToString() == "Andhra Pradesh")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Hyderabad");
                cmbCities.Items.Add("Guntur");
                cmbCities.Items.Add("Vijayawada");
                cmbCities.SelectedValue = 0;

            }
            else if (cmbStates.SelectedItem.ToString() == "Tamilnadu")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Chennai");
                cmbCities.Items.Add("Coimbatore");
                cmbCities.Items.Add("ooty");
                cmbCities.SelectedValue = 0;

            }
            else if (cmbStates.SelectedItem.ToString() == "Karnataka")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Bangalore");
                cmbCities.Items.Add("Mangalore");
                cmbCities.SelectedValue = 0;

            }
            else
            {
                MessageBox.Show("Please Select any value");
            }
        }
    }
}