473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binding datasources to dropdownlist

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

}
Jun 27 '08 #1
1 906
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" <ta*****@gmail.comwrote in message
news:c3**********************************@e39g2000 hsf.googlegroups.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()));
}

}

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: swethasivaram | last post by:
Hi all I am displaying some information from my database using DetailsView which is bound to SqlDataSource1. <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"...
0
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
1
by: Jim Heavey | last post by:
Can I bind 3 controls to 3 different datatables? I have a datagrid with a databound column and 2 DropDownList. I set the datasource for the datagrid to Table1 and the DataBoundColumn to...
1
by: Harold | last post by:
Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something Here are 2 methods. One is the DataGrid's ItemDataBound event to catch...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
9
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
2
by: bmayer | last post by:
I am using a detail view and binding it to a sql data source (at some point in the future it will be an object data source). I have seen the article "Working with Data is ASP.NET 2.0 ::...
2
by: Igor | last post by:
Hi I build a page that has a dropdownlist control bound with data from a database. When user select an item from the dropdownlist I want a label to be filled with a result from a stored...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.