在 C# ASP.NET 中使用下拉列表

发布于 2024-11-17 06:41:14 字数 5788 浏览 7 评论 0 原文

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

我的代码:

命名空间 DynamicWebApplication { 公共部分类 RentForm :System.Web.UI.Page { 公共静态布尔标志= false; IBLClient frontEnd = new FrontEnd();

    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";
    //        }
    //    }
    //}

}

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

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

I have 3 dropdownlists and one image. First ddl (Stamps: BMW, AUDI, VAZ) gets his data from SQL Server and sets selectedIndex = 0. Second ddl (Models: A6, TT, Z4, X5 etc...) depends from first ddl. If User select index = 0 (AUDI) in first ddl then second ddl shows A6 and TT. Third ddl (Colors) depends from second ddl. If User select index = 0 (AUDI) in first ddl and select index = 1 (TT) in second ddl then third ddl shows Blue and Silver. Image depends from all three ddl. How to realize it?

My code:

namespace DynamicWebApplication
{
public partial class RentForm : System.Web.UI.Page
{
public static bool flag = false;
IBLClient frontEnd = new FrontEnd();

    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";
    //        }
    //    }
    //}

}

}

I don't understand what's wrong here. I used AutoPostBack. If I select item from ddl I get empty ddls.

Sorry for my question. I found solution to my problem. It was little thing. I forgot to set EnableViewState="true" in Master Page :( Therefore I got empty ddls when selected items (ddls not fire events)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝有风,海豚有海 2024-11-24 06:41:14

您必须使用 AutoPostBack OnSelectedIndexChanged 属性。 dropdownlist.aspx" rel="nofollow">DropDownList 控件。

示例

后面的标记

<%@ 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.
        }
    }
}

You have to use the AutoPostBack and OnSelectedIndexChanged properties of the DropDownList control.

An example

Markup

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

Code behind

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.
        }
    }
}
维持三分热 2024-11-24 06:41:14

作为一个想法:

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

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

As an idea:

Put those ddls in an UpdatePanel and set AutoPostBack to true on them. Then in the OnChange event handler for each of the two first ddl's clear the items collection of the following.

Fill the items in the ddl's in the OnPreRender event - but only if the list is empty - and make the contents to be filled corresponding to the selected item of the previous ddl.

寂寞美少年 2024-11-24 06:41:14

您想要的只是级联下拉列表。您可以使用 Ajax Control Toolkit,它具有 级联下拉列表控件或者您可以使用下拉列表的选定索引更改事件来实现您想要的,看看这个 文章 它提供了所有代码。

All you want is cascading Dropdownlists. You can either use Ajax Control Toolkit which has a Cascading dropdownlist control or you can use the Selected index change event of the dropdownlist to achieve what you want, have a look on this article it gives all the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文