我要做一个dropdownlist联动,在SelectedIndexChange里写的代码,当第一个drop动的时候用代码个第二个填充内容,代码如下:
 protected void DropDownListUnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(connectionString);
        myConnection.Open();
        String SQLconn = "select * from department where unitid="???????????
        SqlCommand myCommand = new SqlCommand(SQLconn, myConnection);
        SqlDataReader myReader = myCommand.ExecuteReader();
        myReader.Read();
        DropDownList myDD = DetailsViewMove.FindControl("DropDownListDepartmentList") as DropDownList;
        if (myDD != null)
        {
            myDD.Items.Clear();
            myDD.Items.Add(new ListItem(myReader["部门名称"].ToString()));
        }
    }
现在请问,我的查询语句怎么写才能通过unitid来判断我第二个dropdownlist里应该填充什么?
String SQLconn = "select * from department where unitid="???????????
就是这句!!

解决方案 »

  1.   

    "select * from department where unitid=“你选择的id”
      

  2.   

    什么意思?看不懂,是有两个dropdownlist,然后你在其中一个选择了一些东西,然后根据第一个选择的内容需要改变第二个绑定的内容吗?
      

  3.   

    我现在就是不知道"select * from department where unitid=“你选择的id”
    这个"你选择的id"怎么写才提问的... ...
      

  4.   

    String SQLconn = "select * from department where unitid="第一个dropdownlist的值
      

  5.   

    什么意思?看不懂,是有两个dropdownlist,然后你在其中一个选择了一些东西,然后根据第一个选择的内容需要改变第二个绑定的内容吗?没错啊~~
    第一个是单位,第二个是部门,怎么写就能根据第一个的单位ID改变第二个里面的部门了?~
      

  6.   

    你的问题问得不清不楚的,你的意思是不是在DropDownListUnitList中显示单位列表,在DropDownListDepartmentList中显示部门列表啊,如果是这样的话
    你新建一个类如下:
    public class clsboxOverride
    {
    //This class was constructed to override the fact that there no longer
    //exists an itemdata property for the list boxes in my project
    private string m_sID;
    private string m_sName;

    public clsboxOverride(string sID,string sName)
    {
    m_sID = sID;
    m_sName = sName;
    }

    public string ID
    {
    set
    {
    m_sID = value;
    }

    get
    {
    return m_sID;
    }
    }

    public string Name
    {
    set
    {
    m_sName = value;
    }

    get
    {
    return m_sName;
    }
    } public override string ToString()
    {
    return m_sName;
    }

    }
    把这个类作为一个对象添加到DropDownListUnitList中,然后unitid就可以在selectItem中获取了
      

  7.   

    你是说你选择的那个text不是你的ID,对吧?把所有的text和那个text对应的value对应起来用一个hashtable装不就可以了.
      

  8.   

    看不懂啊,如果是想拿你要的值select * from department where unitid in(遍历DropDownListUnitList拿到的值)
      

  9.   

    我现在最想请教各位的是
    String SQLconn = "select * from department where unitid="第一个dropdownlist的值
    是应该怎么写呢?~~还望赐教
      

  10.   

    首先你是否把unitid这个信息存放到dropdownlist,如果没有的话,无法在selectedindexchanged事件中获得此信息。所以你首先需要在绑定此dropdownlist的时候,用此unitid去绑定其的valuemember,否则一切都白搭。
      

  11.   

    String SQLconn = "select * from department where unitid='"+DropDownListUnitList.SelectedValue +"'"
      

  12.   

    我想到了用selectedvalue,可是不知道该怎么用,14楼的方法我用了,似乎不太好用.请问还有别的方法来调用selectedvalue的方法吗?~
      

  13.   

    ID 在数据库中数据类型为字符型的:
    string sqltr=string.format("select * from department where unitid='{0}'",第一个dropdownlist的值)
    在数据库中类型为整型:
    string sqltr=string.format("select * from department where unitid={0}",第一个dropdownlist的值)dropdownlist的值
    如果采用数据绑定的形式获得dropdownlist的列表值,那么取 SelectedValue你的代码是用item.add获得的,取SelectedItem.Tostring  或SelectedItem.SubItems[0].Text
      

  14.   

    请将DropDownList的AutoPostBack的属性设为true
      

  15.   

    Knight94(愚翁) 
    首先你是否把unitid这个信息存放到dropdownlist,如果没有的话,无法在selectedindexchanged事件中获得此信息。所以你首先需要在绑定此dropdownlist的时候,用此unitid去绑定其的valuemember,否则一切都白搭。---------------------------------------
    同意此观点ps:CSDN怎么显示楼层?还是楼主一路数下来的
      

  16.   

    已绑定了selectedvalue
    --------------------------
    回答18楼.是数的.不过是从下往上数.因为能显示总层数
      

  17.   

    to 已绑定了selectedvalue那你把
    String SQLconn = "select * from department where unitid="???????????
    改为
    String SQLconn = "select * from department where unitid=" + yourList.SelectedValue.ToString()
      

  18.   

    String SQLconn = "select * from department where unitid="+DropDownListUnitList.SelectedValue;
      

  19.   

    提供个思路,先把数据读到XML再绑到下一级的DROPDOWNLIST。