Open a new listbox from another listbox .net asp

时间:2016-04-15 11:09:19

标签: c# asp.net .net

I am very new to asp .net development and c#. I have created two list boxes using and namned them. My first list box will have a number of ListItems. I would like that when i press one item on my listbox, my other listbox will load some specific items. Let's say that my first listbox contains a number of cars. When i click on one car my other listbox will show all the components of that car i picked. I appriciate all the help i can get.

2 个答案:

答案 0 :(得分:0)

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

我创建了一个带有两个列表框listbox1和listbox2的winform,这就是我的Form1.cs的样子:

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;

这是InitializeComponent()代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"        
        android:orientation="horizontal"
        android:weightSum="10">

        <TextView
            android:id="@+id/lblKayitNo"
            style="@style/NoviLabel"
            android:layout_width="0dip"
            android:layout_height="35dp"
            android:layout_weight="4"
            android:gravity="center_vertical|left"
            android:text="Kayıt No" />

        <EditText
            android:id="@+id/txtKayitNo"

            android:layout_width="0dip"
            android:layout_height="35dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="6"
            android:enabled="false"
            style="@style/DefaultEditTextSmall"
            android:inputType="textCapCharacters"
            android:singleLine="true" />

    </LinearLayout>

</LinearLayout>

答案 1 :(得分: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);

        }
    }
}