Binding datasources to dropdownlist

T

taa

I'm trying to fill a dropdownlist using the datasource and bind,
however the item I added in advance manually disabpears when the data
is bound. Is there a way to keep the item in the control?
I'm using the code bellow, but I would like to use the outcommented
code instead of the foreach-loop. Ideas?


protected void FillLoadBox(object sender, EventArgs e)
{
DropDownList1.Items.Clear();
DropDownList1.Items.Add(new ListItem("New program...", "-1"));
var UserPrograms = from sp in context.SimplePrograms
select new
ListItem(sp.Name,sp.SimpleProgramId.ToString());

//DropDownList1.DataSource = UserPrograms;
//DropDownList1.DataBind();
foreach (var program in UserPrograms)
{
DropDownList1.Items.Add(new ListItem(program.Name,
program.Id.ToString()));
}

}
 
E

Eliyahu Goldin

protected void FillLoadBox(object sender, EventArgs e)
{
var UserPrograms = from sp in context.SimplePrograms
select new
ListItem(sp.Name,sp.SimpleProgramId.ToString());
DropDownList1.DataSource = UserPrograms;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("New program...", "-1"));
}


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top