在C#ASP.NET中使用dropdownlist

本文关键字:dropdownlist C#ASP NET | 更新日期: 2023-09-27 18:00:23

我有3个下拉列表和一个图像。第一个ddl(Stamps:BBMW,AUDI,VAZ)从SQL Server获取数据并设置selectedIndex = 0。第二个ddl(型号:A6、TT、Z4、X5等)取决于第一个ddl。如果用户在第一个ddl中选择index = 0(AUDI),则第二个ddl显示A6和TT。第三个ddl(颜色)取决于第二个ddl。如果第一个ddl中的用户select index = 0(AUDI)和第二个ddl的select index = 1(TT),则第三个ddl显示蓝色和银色。图像取决于所有三个ddl。如何实现?

我的代码:

命名空间DynamicWebApplication{公共分部类RentForm:System.Web.UI.Page{public static bool flag=false;IBLClient前端=新前端();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            StampOfCarsDropDownList.DataSource = SqlDataSource1;
            StampOfCarsDropDownList.DataValueField = "Stamp";
            StampOfCarsDropDownList.DataTextField = "Stamp";
            StampOfCarsDropDownList.DataBind();
            if (StampOfCarsDropDownList.SelectedIndex == -1)
            {
                StampOfCarsDropDownList.SelectedIndex = 0;
                ModelOfCarsDropDownList.SelectedIndex = 0;
                ColorOfCarsDropDownList.SelectedIndex = 0;
            }
            ListItem stampOfCar = StampOfCarsDropDownList.Items[StampOfCarsDropDownList.SelectedIndex];
            string stamp = stampOfCar.Text;
            foreach (RentCar c in frontEnd.GetListOfModels(stamp))
            {
                ListItem temp = new ListItem(c.Model);
                if (!ModelOfCarsDropDownList.Items.Contains(temp))
                    ModelOfCarsDropDownList.Items.Add(temp);
            }
            ListItem modelOfCar = ModelOfCarsDropDownList.Items[ModelOfCarsDropDownList.SelectedIndex];
            string model = modelOfCar.Text;
            foreach (RentCar c in frontEnd.GetListOfModels(stamp))
            {
                ListItem temp = new ListItem(c.Color);
                if ((c.Model == model) && (!ColorOfCarsDropDownList.Items.Contains(temp)))
                    ColorOfCarsDropDownList.Items.Add(temp);
            }
            //UploadImage(stamp, model, ColorOfCarsDropDownList.Items[ColorOfCarsDropDownList.SelectedIndex].Text);
        }
    }
    protected void ColorOfCarsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem stampOfCar = StampOfCarsDropDownList.Items[StampOfCarsDropDownList.SelectedIndex];
        string stamp = stampOfCar.Text;
        ListItem modelOfCar = ModelOfCarsDropDownList.Items[ModelOfCarsDropDownList.SelectedIndex];
        string model = modelOfCar.Text;
        ListItem colorOfCar = ColorOfCarsDropDownList.Items[ColorOfCarsDropDownList.SelectedIndex];
        string color = colorOfCar.Text;
        //UploadImage(stamp, model, color);
    }
    protected void ModelOfCarsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ColorOfCarsDropDownList.Items.Clear();
        ColorOfCarsDropDownList.SelectedIndex = 0;
        ListItem stampOfCar = StampOfCarsDropDownList.Items[StampOfCarsDropDownList.SelectedIndex];
        string stamp = stampOfCar.Text;
        ListItem modelOfCar = ModelOfCarsDropDownList.Items[ModelOfCarsDropDownList.SelectedIndex];
        string model = modelOfCar.Text;
        foreach (RentCar c in frontEnd.GetListOfModels(stamp))
        {
            ListItem temp = new ListItem(c.Color);
            if (c.Model == model)
                ColorOfCarsDropDownList.Items.Add(temp);
        }
        //UploadImage(stamp, model, ColorOfCarsDropDownList.Items[ColorOfCarsDropDownList.SelectedIndex].Text);
    }
    protected void StampOfCarsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ModelOfCarsDropDownList.Items.Clear();
        ModelOfCarsDropDownList.SelectedIndex = 0;
        ColorOfCarsDropDownList.Items.Clear();
        ColorOfCarsDropDownList.SelectedIndex = 0;
        ListItem stampOfCar = StampOfCarsDropDownList.Items[StampOfCarsDropDownList.SelectedIndex];
        string stamp = stampOfCar.Text;
        foreach (RentCar c in frontEnd.GetListOfModels(stamp))
        {
            ListItem temp = new ListItem(c.Model);
            if (!ModelOfCarsDropDownList.Items.Contains(temp))
                ModelOfCarsDropDownList.Items.Add(temp);
        }
        ListItem modelOfCar = ModelOfCarsDropDownList.Items[ModelOfCarsDropDownList.SelectedIndex];
        string model = modelOfCar.Text;
        foreach (RentCar c in frontEnd.GetListOfModels(stamp))
        {
            ListItem temp = new ListItem(c.Color);
            if (c.Model == model)
                ColorOfCarsDropDownList.Items.Add(temp);
        }
    //    UploadImage(stamp, model, ColorOfCarsDropDownList.Items[ColorOfCarsDropDownList.SelectedIndex].Text);
    }
    //protected void UploadImage(string stamp, string model, string color)
    //{
    //    byte[] fileBytes;
    //    foreach (RentCar c in frontEnd.GetListOfModels(stamp))
    //    {
    //        if ((c.Model == model) && (c.Stamp == stamp) && (c.Color == color))
    //        {
    //            fileBytes = (byte[])c.CarImage.ToArray();
    //            Stream msIn = new MemoryStream(fileBytes);
    //            msIn.Write(fileBytes, 0, fileBytes.Length);
    //            System.Drawing.Image im = System.Drawing.Image.FromStream(msIn);
    //            im.Save("C:/Users/Nickolay/Documents/Visual Studio 2010/Projects/RentCars/DynamicWebApplication/DBImages/CarImage" + c.Stamp + c.Model + c.Color + ".jpeg");
    //            carImage.ImageUrl = "DBImages/CarImage" + c.Stamp + c.Model + c.Color + ".jpeg";
    //        }
    //    }
    //}
}

}

我不明白这里出了什么问题。我使用了AutoPostBack。如果我从ddl中选择项目,我会得到空的ddl。

对不起我的问题。我找到了解决问题的办法。这是一件小事。我忘记在母版页中设置EnableViewState="true":(因此,当选择项目时,我得到了空的ddls(ddls不是激发事件)

在C#ASP.NET中使用dropdownlist

您必须使用DropDownList控件的AutoPostBackOnSelectedIndexChanged属性。

一个例子

标记

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="Test.Default" %>
<!DOCTYPE html>
<html>
    <head runat="server">
        <title>Test</title>
    </head>
    <body>
        <form runat="server">
            <h1>Test</h1>
            <asp:DropDownList ID="First"
                              AutoPostBack="true"
                              DataTextField="FieldA"
                              DataValueField="FieldB"
                              OnSelectedIndexChanged="First_SelectedIndexChanged"
                              runat="server" />
            <asp:DropDownList ID="Second"
                              AutoPostBack="true"
                              DataTextField="FieldA"
                              DataValueField="FieldB"
                              OnSelectedIndexChanged="Second_SelectedIndexChanged"
                              runat="server" />
        </form>
    </body>
</html>

背后的代码

namespace Test
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    public partial class Default : Page
    {
        protected void Page_Load(Object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.First.DataSource = null;
                this.First.DataBind();
            }
        }
        protected void First_SelectedIndexChanged(Object sender, EventArgs e)
        {
            String selectedValue = (sender as DropDownList).SelectedValue;
            // Use dataValue when getting the data for the second drop down.
            this.Second.DataSource = null;
            this.Second.DataBind();
        }
        protected void Second_SelectedIndexChanged(Object sender, EventArgs e)
        {
            // Do things here.
        }
    }
}

作为一个想法:

将这些ddl放在UpdatePanel中,并将它们的AutoPostBack设置为true。然后,在前两个ddl的OnChange事件处理程序中,清除以下各项的items集合。

在OnPreRender事件中填充ddl中的项目(但仅当列表为空时),并使要填充的内容与上一个ddl的选定项目相对应。

您只需要级联Dropdownlists。您可以使用具有级联下拉列表控件的Ajax Control Toolkit,也可以使用下拉列表的Selected index change事件来实现您想要的内容,看看这篇文章,它提供了所有代码。