Binding datasources to dropdownlist

Binding datasources to dropdownlist

am 16.04.2008 12:03:14 von 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()));
}

}

Re: Binding datasources to dropdownlist

am 16.04.2008 15:00:24 von 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


"taa" wrote in message
news:c3ad7000-4bbf-4fa4-b962-0dbc25514afd@e39g2000hsf.google groups.com...
> 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()));
> }
>
> }