0

I am entering in Floor numbers to a DropDownList within a DetailsView Template Field. The floor count is variable and is entered from a prior DDL depending on the amount of floors in a chosen building. Once the amount of floors is passed, it should iterate through numbers until the floor number is reached. In this instance, there are 15 floors. I am doing it like this:

     int i;
            int cnt = Convert.ToInt32(tmpBox3.SelectedItem.Text); //15

            for (i = 0; i <= cnt; i++)
            {
                tmpBox3.Items.Insert(i, new ListItem(i.ToString(), i.ToString()));
            }

            tmpBox3.Items.Remove(new ListItem("0"));  //remove zero
            tmpBox3.Items.Remove(new ListItem(cnt.ToString())); //remove duplicate 15
            tmpBox3.Items.Insert(0, new ListItem("--Select--","0")); //add select
            tmpBox3.SelectedIndex = 0; // make select default choice

Here's the output and the problem. Sorry for the goofy tags

[option selected="selected" value="0"]--Select--[/option]
[option value="1"]1[/option]
[option value="2"]2[/option]...
[option value="14"]14[/option]
[option value="1"]15[/option]

The last entry value is the issue. I can't seem to make that 15. Any tips are most welcome.

Sean
  • 163
  • 1
  • 2
  • 11
  • Silly question - if you're starting your loop that populates the list at 0, then immediately afterwards removing the entry with floor 0, why not start your `for` loop with `i = 1`? Similarly, why not change the termination condition `i <= cnt` to `i < cnt`, that way you won't get an entry for 15 that you seem to be removing later? – Bridge May 24 '12 at 15:11
  • It's a fair question. I had done just that already and still do not get the results I am looking for. If I don't perform the Item.Remove, then I get 2 entries for 15. One is correct and the other with a value of 1. I need to see a single entry for 15 (or whatever number) with the same value. – Sean May 24 '12 at 15:23

2 Answers2

0

If I understand your problem, couldn't you do it this way...

int i; 

// Substract 1 to eliminate last floor
int cnt = Convert.ToInt32(tmpBox3.SelectedItem.Text) - 1; 

tmpBox3.Items.Add(0, new ListItem("--Select--","0")); //add select 

// Notice starting at 1 instead of 0
for (i = 1; i <= cnt; i++) 
{ 
     tmpBox3.Items.Add(i, new ListItem(i.ToString(), i.ToString())); 
} 

tmpBox3.SelectedIndex = 0; // make select default choice 

or another way...

int i;  

int cnt = Convert.ToInt32(tmpBox3.SelectedItem.Text);  

tmpBox3.Items.Add(0, new ListItem("--Select--","0")); //add select  

// Notice starting at 1 instead of 0 and make "less than" instead of "less than or equal"
for (i = 1; i < cnt; i++)  
{  
     tmpBox3.Items.Add(i, new ListItem(i.ToString(), i.ToString()));  
}  

tmpBox3.SelectedIndex = 0; // make select default choice  
Gene S
  • 2,735
  • 3
  • 25
  • 35
  • Thanks for the suggestions mate. Unfortunately, I still get the 1 and 15 on both accounts. – Sean May 24 '12 at 15:21
0

First of all, why do you add item 0 if you don't want it? Make that something like

for (i = 1; i <= cnt; i++)
{
  string floor = i.ToString();
  tmpBox3.Items.Add(new ListItem(floor, floor));
}

Secondly, I guess your tmpBox3 just is not empty at the start, so after you get the tmpBox3.SelectedItem.Text, do a

tmpBox3.Items.Clear();
linac
  • 532
  • 6
  • 14
  • Excellent! First; sorry, I just put up one of the 20 ways I tried to get this going. Second. Thanks! That did the trick. I couldn't see the forest for all the trees in the way. – Sean May 24 '12 at 15:43
  • Final for anyone who wants it: int i; int cnt = Convert.ToInt32(tmpBox3.SelectedItem.Text); tmpBox3.Items.Clear(); for (i = 1; i <= cnt; i++) { string floor = i.ToString(); tmpBox3.Items.Add(new ListItem(floor, floor)); } tmpBox3.Items.Insert(0, new ListItem("--Select--","0")); tmpBox3.SelectedIndex = 0; – Sean May 24 '12 at 15:46