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 はイベントを発生させません)