Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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# code


SelectedIndexChanged

protected 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();


               }


           }


What I have tried:

Clearselection method, clear items method before filling.

And this method
public 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;
}
}
}
}
Posted
Updated 5-Mar-18 23:55pm

You should remove the duplicates before binding the collection to the dropdownlist.

In all honestly if you don't want duplicates in your collection, maybe you should examine the mechanism that adds them to the database, and prevent them from being duplicated (if that's possible).
 
Share this answer
 
Comments
Martin Lauritsen 6-Mar-18 6:00am    
i triede that, with the method above. i callede the method as the first thing in the SelectedIndexChanged event. but it didtn work, it keeps duplicating items
I'm not sure i understand you well... But, if you would like to get unique data, you can use Distinct() method[^]:

C#
var bikeSizes = xdoc.Descendants("BikeType")
    .Where(x => x.Element("TypeName").Value == biketype)
    .SelectMany(x => x.Descendants("SizeName").Select(y => y.Value))
    .Distinct() //here!
    .ToList();
 
Share this answer
 
Comments
Martin Lauritsen 6-Mar-18 6:03am    
I have an dropdown, that every time i call the SelectedIndexChanged event, it duplicates items, so if i have 123 then i call SelectedIndexChanged and it becomes 123123, it keeps adding values to all dropdowns.
Martin Lauritsen 6-Mar-18 6:04am    
distinct did not work
Maciej Los 6-Mar-18 6:15am    
It works as expected. Your code is not working as you wish. Debug it to find out where you've made mistake(s).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900