tags:

views:

41

answers:

4

Is there a way of adding the values 1-15 to an asp dropdownlist without having to do each one individually...

I currently have:

ddlAdults.Items.Insert(0, new listitem("1", "1"))
ddlAdults.Items.Insert(1, new listitem("2", "2"))
ddlAdults.Items.Insert(2, new listitem("3", "3"))
ddlAdults.Ite......

...etc but there has to be a better way.

+1  A: 
for(int i=0;i<15;i++)
{
   ddlAdults.Items.Insert(i, new ListItem((i+1).toString(), (i+1).toString()));
}
Andrei Bularca
+7  A: 
ddlAdults.DataSource = Enumerable.Range(1, 15)
ddlAdults.DataBind()
Anthony Pegram
Left-field, I like it!
James McCormack
Note Enumerable.Range() is only supported in .NET 3.5 onwards
James McCormack
+2  A: 
For i As Integer = 1 To 15
    ddlAdults.Items.Add(new ListItem(i.ToString(), i.ToString()))
Next i
LukeH
A: 

Use a loop?

Like for loop or a foreach loop.

http://en.wikipedia.org/wiki/For_loop

or http://en.wikipedia.org/wiki/Foreach

That should help u, as I dont know what language u're programming in..

Emerion
Tags say VB.NET
Anthony Pegram