本文介绍了级联下拉列表和重复项目的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个问题,我的动态下拉列表会产生重复的项目。 我选择了更改事件,我根据下拉数字1中选择的内容填写下拉数字2。但是每一个我填写它的时间,它是双重内容。我想我已经尝试了一切。 C#代码Hi, I have a problem that my dynamic dropdowns make duplicate items.I selected change event, where I fill in the dropdown number 2 based on what is selected in dropdown number 1. But every time I fill it, it makes double content. And I think I've tried everything.C# codeSelectedIndexChangedprotected void ddl_SelectedIndexChanged(object sender, EventArgs e) { //PlaceHolder1.Controls.Clear(); XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("./xmlReBikeInfo/InfoBikeResponse.xml")); Table table = (Table)PlaceHolder1.FindControl("Table1"); if (table != null) { for (int i = 0; i < Rows; i++) { DropDownList ddl = (DropDownList)PlaceHolder1.FindControl("ddlBike_" + i + ""); //Use Request objects for getting the previous data of the dynamic textbox string biketype = ddl.SelectedValue; HiddenField hdnSize = (HiddenField)PlaceHolder1.FindControl("hdnSize_" + i + ""); DropDownList ddlSize = (DropDownList)PlaceHolder1.FindControl("ddlSize_" + i + ""); ddlSize.CssClass = "dropdown"; //ddlSize.SelectedValue = ""; ddlSize.Visible = true; TextBox txt = (TextBox)PlaceHolder1.FindControl("txtPrice_" + i + ""); HiddenField hdnCykelPris = (HiddenField)PlaceHolder1.FindControl("txtHdnCykelPris_" + i); HiddenField hdnAccPris = (HiddenField)PlaceHolder1.FindControl("txtHdnAccPris_" + i); string number1; string number2; XDocument xdoc = XDocument.Load(Server.MapPath("./xmlReBikeInfo/InfoBikeResponse.xml")); var bikeSizes = xdoc.Descendants("BikeType") .Where(x => x.Element("TypeName").Value == biketype) .SelectMany(x => x.Descendants("SizeName").Select(y => y.Value)) .ToList(); var bikePrice = xdoc.Descendants("BikeType") .Where(x => x.Element("TypeName").Value == biketype) .SelectMany(x => x.Descendants("TypePriceVat").Select(y => y.Value)) .ToList(); var bikeAcc = xdoc.Descendants("Accessorie") .Where(x => x.Element("AccessorieName").Value == biketype) .SelectMany(x => x.Descendants("AccessoriePriceVat").Select(y => y.Value)) .ToList(); decimal result = 0; if (biketype == "0") { txt.Text = ""; } if (biketype == "Touring") { foreach (string s in bikeSizes) { ddlSize.Items.Add(new ListItem(s + "")); } foreach (string p in bikePrice) { hdnCykelPris.Value = p.ToString(); string accPrice = ""; if (hdnAccPris.Value == "") { accPrice = "0"; } else { accPrice = hdnAccPris.Value; } number1 = hdnCykelPris.Value; number2 = accPrice.ToString(); result = decimal.Parse(number1) + decimal.Parse(number2); txt.Text = result.ToString(); //txt.Text = hdnCykelPris.Value; //litSum.Text = txt.Text; } ddlSize.Visible = true; } if (biketype == "Junior Bike") { foreach (string s in bikeSizes) { ddlSize.Items.Add(new ListItem(s + "")); } foreach (string p in bikePrice) { hdnCykelPris.Value = p.ToString(); string accPrice = ""; if (hdnAccPris.Value == "") { accPrice = "0"; } else { accPrice = hdnAccPris.Value; } number1 = hdnCykelPris.Value; number2 = accPrice.ToString(); result = decimal.Parse(number1) + decimal.Parse(number2); txt.Text = result.ToString(); //txt.Text = hdnCykelPris.Value; } } if (biketype == "MTB") { foreach (string s in bikeSizes) { ddlSize.Items.Add(new ListItem(s + "")); } foreach (string p in bikePrice) { hdnCykelPris.Value = p.ToString(); string accPrice = ""; if (hdnAccPris.Value == "") { accPrice = "0"; } else { accPrice = hdnAccPris.Value; } number1 = hdnCykelPris.Value; number2 = accPrice.ToString(); result = decimal.Parse(number1) + decimal.Parse(number2); txt.Text = result.ToString(); //txt.Text = hdnCykelPris.Value; } } if (biketype == "Classic E") { foreach (string s in bikeSizes) { ddlSize.Items.Clear(); ddlSize.Items.Add(new ListItem(s + "")); //ddlSize.ClearSelection(); } ddlSize.Visible = true; foreach (string p in bikePrice) hdnCykelPris.Value = p.ToString(); string accPrice = ""; if (hdnAccPris.Value == "") { accPrice = "0"; } else { accPrice = hdnAccPris.Value; } number1 = hdnCykelPris.Value; number2 = accPrice.ToString(); result = decimal.Parse(number1) + decimal.Parse(number2); txt.Text = result.ToString(); } } 我尝试了什么: Clearselection方法,填充前清除项目方法。 这个方法What I have tried:Clearselection method, clear items method before filling.And this methodpublic static void RemoveDuplicateItems(DropDownList ddl){for (int i = 0; i < ddl.Items.Count; i++){ddl.SelectedIndex = i;string str = ddl.SelectedItem.ToString();for (int counter = i + 1; counter < ddl.Items.Count; counter++){ddl.SelectedIndex = counter;string compareStr = ddl.SelectedItem.ToString();if (str == compareStr){ddl.Items.RemoveAt(counter);counter = counter - 1;}}}}推荐答案var bikeSizes = xdoc.Descendants("BikeType") .Where(x => x.Element("TypeName").Value == biketype) .SelectMany(x => x.Descendants("SizeName").Select(y => y.Value)) .Distinct() //here! .ToList(); 这篇关于级联下拉列表和重复项目的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 10:47