WebForm中,设置DropDownList1,Dropdownlist2,and ListBox1;DropDownList1用来读取数据库中大类内容;Dropdownlist2读取小类内容,ListBox1用来显示小类下的相关数据内容。现在的问题是:DropDownList1读取了大类,Dropdownlist2显示了相关的小类;但是Dropdownlist2无法更改SelectedItem;如果将Dropdownlist2的AutoPostBack改为False;可以选取,但是无法把改变传递到ListBox1;
请问各位高手该如何解决?

解决方案 »

  1.   

    代码如下:
                               if(!Page.IsPostBack)
    {
    //DropDownList1
    try
    {
    cmd=new SqlCommand("Select cInvCName from InventoryClass where bInvCEnd=0 or(LEN(cInvCCode)=2 and bInvCEnd=1)",Conn);
    reader=cmd.ExecuteReader();
    while(reader.Read())
    {
    ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
    DropDownList1.Items.Add(item);
    }
    }
    catch(Exception exc)
    {
    this.Response.Write(exc.Message);
    }
    finally
    {
    reader.Close();
    }
    }
    else 
    {

    //DropDownList2
    Dropdownlist2.Items.Clear();


    try
    {
    string change=DropDownList1.SelectedItem.Text;
    cmd=new SqlCommand("Select cInvCCode from InventoryClass where cInvCName='"+change+"'",Conn);
    reader=cmd.ExecuteReader();
    reader.Read();
    string code=reader.GetValue(0).ToString();
    reader.Close();
    cmd=new SqlCommand("select cInvCName from InventoryClass where substring(cInvCCode,1,LEN('"+code+"'))='"+code+"' and bInvCEnd=1",Conn);
    reader=cmd.ExecuteReader();

    while(reader.Read())
    {
    ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
    Dropdownlist2.Items.Add(item);
    }
    }
    catch(Exception exc)
    {
    this.Response.Write(exc.Message);
    }
    finally
    {
    reader.Close();
    }


    //ListBox1
    if(Dropdownlist2.Items.ToString()!="")
    {
    ListBox1.Items.Clear();
    //dropdownlist2.selectedItem.Text(Name)->cInvCCode->cInvName
    string change=Dropdownlist2.SelectedItem.Text;
    cmd=new SqlCommand("Select cInvCCode from InventoryClass where cInvCName='"+change+"'",Conn);
    reader=cmd.ExecuteReader();
    reader.Read();
    string code=reader.GetValue(0).ToString();
    reader.Close();
    cmd=new SqlCommand("select cInvName from Inventory where cInvCCode='"+code+"'",Conn);
    reader=cmd.ExecuteReader();
    while(reader.Read())
    {
    ListItem item=new ListItem(reader.GetValue(0).ToString(),reader.GetValue(0).ToString());
    ListBox1.Items.Add(item);
    }

    }
    }
      

  2.   

    只需吧DropDownList1的AutoPostBack设为True就行了吗,另外几个的绑定在DropDownList1的SelectedIndexChanged事件中写代码嘛!
      

  3.   

    用个按纽来实现SelectedItem的功能就是了~
      

  4.   

    to:zr1982930(皮卡丘) 
    DropDownList1的AutoPostBack本来就设置的true
    SelectedIndexChanged事件代码我没写,不过用上面的代码已经完全可以实现从DropDownList1到DropDownList2的功能
    现在的问题是DropDownList2的值能显示,但是无法选取,不论选什么值都显示第一行的值。
    to:baobei7758(陵少) 
    不是很清楚你所说的方法,可以说清楚一些吗?
      

  5.   

    我认为是在选取dropdownlist2
    的内容时,重载了一次页面,然后执行了从list1选取list2内容的语句;不知对不对,该如何处理;请各位大侠指点。多谢多谢
      

  6.   

    page_Load写的不对其实可以用另一种思路,参考:
    http://blog.csdn.net/athossmth/archive/2005/03/20/324911.aspx
      

  7.   

    你的page_Load的问题,zr1982930(皮卡丘) 的说法正确。
    DropDownList1和DropDownList2的AutoPostBack都设置为true。
    在page_Load的if(!Page.IsPostBack)中填充DropDownList1;
    在DropDownList1的SelectedIndexChanged事件中填充DropDownList2;
    在DropDownList2的SelectedIndexChanged事件中填充ListBox1。
    你再好好想想。
    个人意见。
      

  8.   

    嗯 刚才去吃饭了,吃饭的时候我也想了,我的写法的确有问题;
    每次提交都会pageload,
    我想selectedindexchanged事件是可行的,我下午试试多谢各位