【问题标题】:Open a new listbox from another listbox .net asp从另一个列表框 .net asp 打开一个新列表框
【发布时间】:2016-04-15 11:09:18
【问题描述】:

我对 asp .net 开发和 c# 非常陌生。我使用并命名了两个列表框。我的第一个列表框将有许多 ListItems。我希望当我按下列表框上的一个项目时,我的另一个列表框将加载一些特定项目。假设我的第一个列表框包含许多汽车。当我点击一辆车时,我的另一个列表框将显示我选择的那辆车的所有组件。我感谢我能得到的所有帮助。

【问题讨论】:

  • 使用 ListBox.SelectedIndexChanged 事件
  • 如果您使用的是 ASP.NET,请标记为 ASP.NET

标签: c# asp.net .net


【解决方案1】:

假设此示例用于类别和项目(与汽车和组件相同)

我创建了一个带有两个列表框 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;

【讨论】:

  • 在他的第一句话中,OP 说:“我对 asp .net 开发和 c# 很陌生”不错的 winforms 示例,但是... :)
【解决方案2】:

嗯,这是一个非常广泛的问题。 但是,这里有一些东西可以帮助您入门。

标记:

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

        }
    }
}

【讨论】:

  • 谢谢!但我在 LoadPartsList(this.lbCars.SelectedValue); 行上得到一个 nullpointerexeption;我找不到错误,有什么提示吗?
  • 看起来 lbCars.SelectedValuenull。在LoadPartsList(this.lbCars.SelectedValue);之前添加if (!string.IsNullOrEmpty(lbCars.SelectedValues))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多