Click to See Complete Forum and Search --> : add values to dropdownlist


jagmit
September 30th, 2009, 11:52 AM
i have a code...

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i <= 100; i++)
{
ListItem itm = new ListItem("i");

DropDownList1.Items.Add(itm);


}
}

I need to add values to a dropdownlist from 1 to 100...
but i get 100 i's displayed..
Please tell me how..
THanks

vandel212
September 30th, 2009, 11:55 AM
i have a code...

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i <= 100; i++)
{
ListItem itm = new ListItem("i");

DropDownList1.Items.Add(itm);


}
}

I need to add values to a dropdownlist from 1 to 100...
but i get 100 i's displayed..
Please tell me how..
THanks

You have your variable i in quotes thus making it a string so it is listing a string 100 times. try removing the quotes.

This would also work

for (int i = 1; i <= 100; i++)
{
DropDownList1.Items.Add(i);
}

darwen
September 30th, 2009, 11:56 AM
Try


for (int i = 1; i <= 100; i++)
{
DropDownList1.Items.Add(i.ToString());
}


Darwen.

jagmit
September 30th, 2009, 12:06 PM
Darwen thanks, it worked just fine..
vandel212 that worked too....

Thanks guys..