我有 3 个下拉列表和一张图片。首先 ddl (Stamps: BMW, AUDI, VAZ) 从 SQL Server 获取他的数据并设置selectedIndex = 0
. 第二个 ddl(型号:A6、TT、Z4、X5 等)取决于第一个 ddl。如果用户index = 0
在第一个 ddl 中选择 (AUDI),则第二个 ddl 显示 A6 和 TT。第三个 ddl(颜色)取决于第二个 ddl。如果第一个 ddl 中的用户select index = 0
(AUDI) 和select index = 1
第二个 ddl 中的 (TT),则第三个 ddl 显示蓝色和银色。图像取决于所有三个 ddl。如何实现?
我的代码:
命名空间 DynamicWebApplication { 公共部分类 RentForm : System.Web.UI.Page { 公共静态布尔标志 = 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 中选择项目,我会得到空的 ddls。
对不起我的问题。我找到了解决我的问题的方法。这是小事。I forgot to set EnableViewState="true" in Master Page :( Therefore I got empty ddls when selected items (ddls not fire events)