我对asp.net开发和c#非常陌生。我已经创建了两个列表框,使用并命名了它们。我的第一个列表框将有许多ListItems。我希望当我按下列表框上的一个项目时,我的另一个列表框会加载一些特定的项目。假设我的第一个列表框包含许多汽车。当我点击一辆车时,我的另一个列表框将显示我选择的那辆车的所有组件。我把我能得到的一切帮助都表示感谢。
假设类别和项目(与汽车和零部件相同)
我创建了一个有两个列表框listbox1和listbox2的winform,这就是我的Form1.cs的样子:
namespace WinFormsApp
{
public partial class Form1 : Form
{
private List<Category> categories;
public Form1()
{
InitializeComponent();
categories = new List<Category>();
var categoryOne = new Category { Name = "Category 1"} ;
categoryOne.Items.Add( new CategoryItem { Name = "Item 1"} );
var categoryTwo = new Category { Name = "Category 2" };
categoryTwo.Items.Add( new CategoryItem { Name = "Item 2" } );
categories.Add( categoryOne );
categories.Add( categoryTwo );
}
private void Form1_Load(object sender, System.EventArgs e)
{
categoryBindingSource.DataSource = categories;
}
}
public class Category
{
public string Name { get; set; }
public List<CategoryItem> Items { get; private set; }
public Category()
{
Items = new List<CategoryItem>();
}
}
public class CategoryItem
{
public string Name { get; set; }
}
}
这是InitializeComponent()代码
this.listBox1.DataSource = this.categoryBindingSource;
this.listBox1.DisplayMember = "Name";
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(24, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(242, 238);
this.listBox1.TabIndex = 0;
this.listBox1.ValueMember = "Items";
this.categoryBindingSource.DataSource = typeof(Category);
this.listBox2.DataSource = this.itemsBindingSource;
this.listBox2.FormattingEnabled = true;
this.listBox2.Location = new System.Drawing.Point(286, 24);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(276, 238);
this.listBox2.TabIndex = 1;
this.listBox2.ValueMember = "Name";
this.itemsBindingSource.DataMember = "Items";
this.itemsBindingSource.DataSource = this.categoryBindingSource;
这是一个非常宽泛的问题。但是,这里有一些东西可以让你开始。
标记:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<asp:ListBox runat="server" id="lbCars" OnSelectedIndexChanged="lbCars_SelectedIndexChanged" Width="200px" Height="100px" AutoPostBack="true"></asp:ListBox>
</p>
<p>
<asp:ListBox runat="server" id="lbParts" Width="200px" Height="100px"></asp:ListBox>
</p>
</asp:Content>
和代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadCarsList();
}
}
protected void lbCars_SelectedIndexChanged(object sender, EventArgs e)
{
//this will be executed when selected item in list of cars is changes
//load parts
LoadPartsList(this.lbCars.SelectedValue);
}
protected void LoadCarsList()
{
//here is the place to load cars list from database.
//for this example, cars are hard-coded
this.lbCars.Items.Clear();
this.lbCars.Items.Add(new ListItem("Peugeot", "1"));
this.lbCars.Items.Add(new ListItem("VW", "2"));
this.lbCars.Items.Add(new ListItem("Ford", "3"));
this.lbCars.Items.Add(new ListItem("Fiat", "4"));
}
protected void LoadPartsList(string carId)
{
//this will be called when you s
this.lbParts.Items.Clear();
this.lbParts.Items.Add("part one for car " + carId);
this.lbParts.Items.Add("part two for car " + carId);
this.lbParts.Items.Add("part three for car " + carId);
}
}
}