Q:

Making drop down list in ASP.Net by foreach loop

we can make dropdown list in asp.net component with below syntax
 <asp:DropDownList ID="test" runat="server">
      <asp:ListItem Text="1" Value="1"></asp:ListItem>
 </asp:DropDownList>

if we want our combo box contain 1 to 1000 , is there any way to populate it with foreach loop , rather than manually add 1000 item to it ?

A:

Yes, you can add ListItems programmatically:
for(int i=1; i<=1000; i++)
{
    ListItem item = new ListItem(i.ToString(), i.ToString());
    test.Items.Add(item);
}

ListItemCollection.Add
You could also use this linq query and use it as DataSource:
var source = Enumerable.Range(1, 1000)
    .Select(i => new { Text= i.ToString(), Value=i.ToString() });
test.DataSource = source;
test.DataTextField = "Text";
test.DataValueField = "Value";
test.DataBind();

