C# 从另一个listbox.net asp打开新的listbox
我对asp.net开发和c#非常陌生。我已经创建了两个列表框并命名它们。我的第一个列表框将包含许多列表项。我希望当我按下列表框上的一个项目时,我的另一个列表框将加载一些特定的项目。假设我的第一个列表框包含许多汽车。当我点击一辆车时,我的另一个列表框将显示我选择的那辆车的所有部件。我感谢所有我能得到的帮助。假设此示例适用于类别和项目(与汽车和部件相同) 我创建了一个winform,其中包含两个列表框listbox1和listbox2,这就是我的Form1.cs的外观:C# 从另一个listbox.net asp打开新的listbox,c#,asp.net,.net,C#,Asp.net,.net,我对asp.net开发和c#非常陌生。我已经创建了两个列表框并命名它们。我的第一个列表框将包含许多列表项。我希望当我按下列表框上的一个项目时,我的另一个列表框将加载一些特定的项目。假设我的第一个列表框包含许多汽车。当我点击一辆车时,我的另一个列表框将显示我选择的那辆车的所有部件。我感谢所有我能得到的帮助。假设此示例适用于类别和项目(与汽车和部件相同) 我创建了一个winform,其中包含两个列表框listbox1和listbox2,这就是我的Form1.cs的外观: namespace WinF
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; }
}
}
这是一个非常广泛的问题。 但是,这里有一些东西可以让你开始 标记:
<%@ 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>
使用ListBox.SelectedIndexChanged EventTag作为ASP.NET,如果这是您在他的第一句话中使用的,OP说:“我对ASP.NET开发和c#非常陌生。”winforms示例不错,但…:)非常感谢。但是我在LineLoadPartsList(this.lbCars.SelectedValue)上得到一个NullPointerExection;我找不到错误,有什么提示吗?看起来像是
lbCars。SelectedValue
是null
。如果(!string.IsNullOrEmpty(lbCars.SelectedValues))在LoadPartsList(this.lbCars.SelectedValues)之前添加代码>
<%@ 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);
}
}
}