如何通过listbox1中的select值(按id)显示listbox2值c#

时间:2011-04-18 14:24:34

标签: c# .net asp.net linq listbox

如何通过listbox1中的select值(按id)在List.net上显示,在asp.net c#...

示例:

listbox1 (car name) :  Fiat , subaro, honda

listbox2 (car type..after fiat selected at box1) : punto sx, punto gx. punto blabla-x (...)

listbox3 (car year...after type selected) : 2000-2002 (....)

listbox4 (items for the car that selected at lstbox1 2 and 3 .. : volta, wheel (...)

我有表格的数据库(sql):CarName,CarType,CarYear,Items

谢谢!

3 个答案:

答案 0 :(得分:1)

一种简单的方法是将第二个列表框绑定到第一个列表框的selectedindexchanged事件中的var。示例(在这种情况下使用dropdownlistbox):

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    var x =
        [LINQ query here]
    DropDownList2.DataSource = x;
    DropDownList2.DataTextField = "[fieldname]";
    DropDownList2.DataValueField = "ID";
    DropDownList2.DataBind();
}

如果您知道如何编写查询并将正确的字段分配给DataTextField(将显示在第二个列表框中的字段),那么您已经完成了设置。

答案 1 :(得分:0)

Try this AjaxControlToolkit控件。

答案 2 :(得分:0)

尝试SelectedIndexChanged事件。只需确保您在Tag中有id。这是一个根据我最近的(WinForms)家庭作业编辑的例子:

private void lstCarName_SelectedIndexChanged(object sender, EventArgs e)
{
    if (lstCarName.SelectedItems.Count > 0)
    {
        int CarId = (int)lstKlanten.SelectedItems[0].Tag;
        MakeCarTypeListBox(id);
    }
}

MakeCarTypeListBox看起来像:

private void MakeCarTypeListBox(int carId)
{
    lstCarType.Items.Clear();
    CarType[] carTypes = CarType.CarTypesByCarNameI(carId);
    for (int i = 0; i < carTypes.Length; i++)
    {
        ListViewItem item = new ListViewItem(carTypes[i].Id.ToString());
        item.SubItems.Add(carTypes[i].CarTypeName);
        item.Tag = carTypes[i].Id;

        lstDetail.Items.Add(item);
    }
}

请记住,我使用的是WinForms和自制的实体类,所以你的代码可能会有些不同......