-1

3 つのドロップダウン リストと 1 つの画像があります。最初の ddl (スタンプ: BMW、AUDI、VAZ) は、SQL Server からデータを取得し、selectedIndex = 0. 2 番目の ddl (モデル: A6、TT、Z4、X5 など) は最初の ddl に依存します。ユーザーindex = 0が最初の ddl で (AUDI) を選択すると、2 番目の ddl に A6 と TT が表示されます。3 番目の ddl (色) は 2 番目の ddl に依存します。ユーザーselect index = 0(AUDI) が最初の ddl で、select index = 1(TT) が 2 番目の ddl の場合、3 番目の ddl は Blue と Silver を示します。画像は 3 つすべての ddl に依存します。それを実現する方法は?

私のコード:

名前空間 DynamicWebApplication { public 部分クラス 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";
    //        }
    //    }
    //}

}

}

ここで何が問題なのかわかりません。AutoPostBack を使用しました。ddl から項目を選択すると、空の ddl が取得されます。

私の質問で申し訳ありません。問題の解決策を見つけました。ちょっとしたことでした。マスター ページで EnableViewState="true" を設定するのを忘れました :( そのため、アイテムを選択すると空の ddls を取得しました (ddls はイベントを発生させません)

4

3 に答える 3

1

AutoPostBackコントロールのおよびOnSelectedIndexChangedプロパティを使用する必要があります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.
        }
    }
}
于 2011-06-24T12:09:19.540 に答える
0

アイデアとして:

それらのddlsをUpdatePanelに配置し、AutoPostBackをtrueに設定します。次に、最初の2つのddlのそれぞれのOnChangeイベントハンドラーで、次のアイテムコレクションをクリアします。

OnPreRenderイベントのddlの項目を入力します(ただし、リストが空の場合のみ)。入力する内容を、前のddlの選択した項目に対応させます。

于 2011-06-24T12:03:26.557 に答える
0

ドロップダウンリストをカスケードするだけです。Cascading ドロップダウンリスト コントロールを備えた Ajax Control Toolkit を使用するか、ドロップダウンリストの Selected index change イベントを使用して目的を達成できます。すべてのコードが記載されているこの記事を参照してください。

于 2011-06-24T12:10:49.997 に答える