Binding datasources to dropdownlist

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()));
}

}
taa [ Mi, 16 April 2008 12:03 ] [ ID #1943434 ]

Re: Binding datasources to dropdownlist

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" <taa1337 [at] gmail.com> wrote in message
news:c3ad7000-4bbf-4fa4-b962-0dbc25514afd [at] 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()));
> }
>
> }
Eliyahu Goldin [ Mi, 16 April 2008 15:00 ] [ ID #1943451 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » Binding datasources to dropdownlist

Vorheriges Thema: The Controls collection cannot be modified because the control
Nächstes Thema: Hiding columns when binding datasource to a gridview