根据下拉列表选择填写另一个下拉列表 [英] Based on Drop down list selection fill another dropdownlist

查看:126
本文介绍了根据下拉列表选择填写另一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据第一个选项填写第二个下拉列表。

i想要使用条件:

例如:

How to fill a second dropdownlist based on the selection made in the first one.
i want to use a condition:
for example:

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
       {

           if (dropdownlis1t.SelectedValue == "1")
           {
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
               con.Open();

               SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= ''3'' OR Index= ''2''", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();

           }
           else
               if (dropdownlist1.SelectedValue == "2")
               {

                   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString);
                   con.Open();

                     SqlCommand cmd = new SqlCommand("Select [Code] from Table where Index= ''1''", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               con.Close();
               dropdownlist2.DataSource = ds;
               dropdownlist2.DataTextField = "Code";
               dropdownlist2.DataValueField = "Code";
               dropdownlist2.DataBind();
               }

推荐答案

根据您的代码......值不会填充到下拉列表中。



according to your code..the values does not populate to the dropdownlist.

dropdownlist2.DataTextField = "Code";
                dropdownlist2.DataValueField = "Code";





因为,它不会保留值,但它只是将值分配给

下拉列表文本字段和值字段。



来填充。这是方法之一



准备数据集;

数据集dv;



because, it will not hold the values but it just assigns the values to the
dropdownlist textfield and value field.

to populate.this is one of the method

prepare the dataset;
dataset dv;

int n = dv.Tables["tblcountry"].Rows.Count;

     DropDownList.Items.Clear();
     DropDownList.Items.Add("--Select--");

     for (int i = 0; i <= n - 1; i++)
     {
         DropDownList.DataSource = dv;
         ListItem li = new ListItem();
         li.Text = dv.Tables["tblcountry"].Rows[i][1].ToString();
         li.Value = dv.Tables["tblcountry"].Rows[i][0].ToString();
         DropDownList.Items.Add(li);
     }



它可以尝试这种方法。


it works try this method.


我认为你需要每当执行一次回调,第一次下拉变化。我写这段代码已经很久了。此链接可能会有所帮助。

http://support.microsoft.com/kb/910450 [ ^ ]
I think you need to perform a callback whenever, the first drop down changes. It''s being a long time I wrote code to do that. This link could be helpful.
http://support.microsoft.com/kb/910450[^]


这篇关于根据下拉列表选择填写另一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆