日期:2014-05-18  浏览次数:20492 次

有關於DropDownList的問題
我用到2個DropDownList,,第2個DropDownList裡的數據是根據第1個來的,所以當第1個是請選擇的時候怎麼清空第2個裡的數據?

------解决方案--------------------
两个DropDownList的联动问题。
 
 已经有很多这方面的帖子了:
http://topic.csdn.net/u/20080328/14/26889f9e-f4c6-42b5-ab94-98e2e262239d.html

希望对你有用!
------解决方案--------------------
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList1.SelectedValue == "请选择")
{
this.DropDownList2.Items.Clear();
}
else
{
// 重新绑定DropDownList2的数据
}
}
------解决方案--------------------
C# code

protected void Page_Load(object sender, EventArgs e)
{
     BindDropDownList1();
     BindDropDownList2(DropDownList1.SelectedValue);
}

private void BindDropDownList1()
    {
        IList<Class1> list = BL.Instance.GetDropDownList1DataSource();
        
        DropDownList1.Items.Add(new ListItem("---请选择---", "-1"));
        foreach (Class1 class in list)
        {
            DropDownList1.Items.Add(new ListItem(class1.text, class.value));
        }
    }

    private void BindDropDownList2(int value)
    {
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("---请选择---", "-1"));
        if (value > 0)        
        {               
            IList<Class2> list = BL.Instance.GetDropDownList2DataSource(value);
            foreach (Class2 class in list)
            {
                DropDownList2.Items.Add(new ListItem(class.text, class.value));
            }
        }
    }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {        
        BindDropDownList2(BindDropDownList1.SelectedValue);
    }

------解决方案--------------------
再在Page_Load里面要加一个if (!IsPostBack) 哈.
------解决方案--------------------
参考:
http://blog.csdn.net/insus/archive/2007/11/11/1878690.aspx

http://download.csdn.net/source/282546