开发者

How to show at listbox2 value by select value from listbox1 (by id) c#

开发者 https://www.devze.com 2023-02-26 05:01 出处:网络
How to show at ListBox value by select value from listbox1 (by id), at asp.net c#... example: listbox1 (car name) :Fiat , subaro, honda

How to show at ListBox value by select value from listbox1 (by id), at asp.net c#...

example:

listbox1 (car name) :  Fiat , subaro, honda

listbox2 (car type..after fiat selected at box1) : punto sx, punto gx. punto blab开发者_JS百科la-x (...)

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

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

i have database (sql) with tables: CarName, CarType , CarYear, Items

thanks!


An easy way is to bind the second listbox to a var in the selectedindexchanged event of the first listbox. Example (using dropdownlistboxes in this case):

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

If you know how to write the query and assign the correct field to the DataTextField (the one that will show up in the 2nd listbox) you're all set.


Try this AjaxControlToolkit control.


Try the SelectedIndexChanged event. Just make sure you have the id in the Tag. Here's an example edited from a recent (WinForms) homework assignment I had:

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

MakeCarTypeListBox could look something like:

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);
    }
}

Bear in mind that I used WinForms and home-made entity classes though, so your code will probably be somewhat different...

0

精彩评论

暂无评论...
验证码 换一张
取 消