2016-04-15 94 views
0

我對asp.net開發和c#很新。我創建了兩個列表框,並使用它們命名。我的第一個列表框將有一些ListItems。我希望當我按下我的列表框中的一個項目時,我的另一個列表框會加載一些特定的項目。假設我的第一個列表框包含一些汽車。當我點擊一輛車時,我的另一個列表框將顯示我選擇的那輛車的所有組件。我熟悉我能得到的所有幫助。從另一個列表框中打開一個新的列表框.net asp

+1

使用ListBox.SelectedIndexChanged事件 – GasDev

+0

標籤爲ASP.NET如果這是你正在使用 – Paparazzi

回答

0

假設這個例子中的類別和項目(同汽車和零部件)

我創建了兩個列表框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; 
+0

在他的第一句話是什麼OP說:「我很新** ** asp.net開發和C#「尼斯Winforms的例子,但... :) – Nino

0

那麼,這是一個很廣泛的問題。 但是,這裏有一些讓你開始的東西。

標記:

<%@ 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); 

     } 
    } 
} 
+0

謝謝!但我得到LoadPartsList(this.lbCars.SelectedValue)行上的nullpointerexeption;我找不到錯誤,提示? –

+0

看起來像'lbCars.SelectedValue'是'null'。在LoadPartsList(this.lbCars.SelectedValue)之前加'if(!string.IsNullOrEmpty(lbCars.SelectedValues))';' – Nino

相關問題