Archana B.R
Archana B.R

Reputation: 407

How to make a CheckBoxList have unique items when a new item is added to it?

I have a DropDownList with items like:

  1. BEDROOM
  2. KITCHEN
  3. BATHROOM
  4. LIVING_HALL......etc..

I have a TextBox which accept only number.

For example if I select BEDROOM from the DropDownList and if I type any number in the TextBox for eg. 2, the selected item in the DropDownList and will get concatenated with the number which is put in a loop, and on button_click items are added one by one to the CheckBoxList like:

  1. BEDROOM1
  2. BEDROOM2

The .cs code I have written is:

protected void Button_Click(object sender, EventArgs e)
    {
            int q1 = Convert.ToInt16(TextBox1.Text);
            for (int i = 1; i <= q1; i++)
            {
                string t1 = DropDownList1.SelectedItem.ToString().Trim();
                CheckBoxList1.Items.Add(string.Concat(t1, i));
            }
            TextBox1.Text = "";
    }

Similarly the same procedure is followed with other items in DropDownList. Like if KITCHEN is selected and number in the TextBox is 3. On button_click,items like:

  1. KITCHEN1
  2. KITCHEN2
  3. KITCHEN3 is added to the CheckBoxList.

From above example my CheckBoxList will look something like:

  1. BEDROOM1
  2. BEDROOM2
  3. KITCHEN1
  4. KITCHEN2
  5. KITCHEN3

So now my problem is after added KITCHEN item in the List, I want to again add one BEDROOM.

So if I select BEDROOM from the DropDownList and type 1 in the TextBox, and on button_click, I want BEDROOM4 to be added to the CheckBoxList. But BEDROOM1 adds again according to my code.

Kindly Kindly help...thank you in advance.

Upvotes: 1

Views: 921

Answers (2)

Andrei
Andrei

Reputation: 56716

Rewrite your handler to something like this:

protected void Button_Click(object sender, EventArgs e)
{
    int q1 = Convert.ToInt16(TextBox1.Text);

    string t1 = DropDownList1.SelectedItem.ToString().Trim();
    int start = 1;
    string checkBoxValue = string.Concat(t1, start);
    while (CheckBoxList1.Items.Cointains(new ListItem(checkBoxValue)))
    {
        start++;
        checkBoxValue = string.Concat(t1, start);
    }

    for (int i = start; i <= start + q1 - 1; i++)
    {
        CheckBoxList1.Items.Add(string.Concat(t1, i));
    }

    TextBox1.Text = "";
}

Essentially we are just checking for each incrementing value whether such an element presents in the check box list collection. As soon as the absent value is found, we are adding the requested range of values, just as before.

Upvotes: 2

Waqar Janjua
Waqar Janjua

Reputation: 6123

Before concatenating a new item first check it exist or not For example you select BEDROOM on the dropdown and there are already 3 BEDROOM items in the dropdownlist. Now first check BEDROOM1 exist in the dropdown or not ? If yes then increment the no from 1 to 2 like BEDROOM1 to BEDROOM2 if it exists then again increment the number...... you can easily check the item as

  bool check = DropDownList1.Items.Contains(new ListItem("name", "value"));
  if( check )
  {
      string t1 = DropDownList1.SelectedItem.ToString().Trim();
        CheckBoxList1.Items.Add(string.Concat(t1, i));
  }
  else
  {
      // again concatenate a new item 
    }

Just add the above line before

Upvotes: 0

Related Questions