473,323 Members | 1,550 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,323 software developers and data experts.

Adding Value to Dropdownlist

In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)

Sep 7 '06 #1
5 17143
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #2
Another way, besides Jerry's:

After the DataBining (you may bind the data all at once by calling
Page.DataBinding() or call DataBinding on each control), you simply insert a
new ListItem at beginning of the list (Index=0):

''Bind data to the dropdownlist first
DropDownList_companies.DataSource=theSource
DropDownList_companies.DataBinding()

Dim NewItem As ListItem = New ListItem("Select Company", "0")
DropDownList_companies.Items.Insert(0, NewItem)
"Jerry C" <je*****@nospam.nospamwrote in message
news:89**********************************@microsof t.com...
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to
stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
>In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I
would
like to add the value "Select Company" which will be the selected value
when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in
the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the
dropdownlist.
How do I add a value to the dropdownlist that is not in the database?
Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)

Sep 7 '06 #3
Thank for the reply Jerry and Norman.

I'm trying to do this the ASP.NET 2.0 way by not binding in the code behind
file. I'm using the sqldatasource control (asp:SqlDataSource) in the aspx
file. Can this be done without binding in the code behind file? Thanks.

"Jerry C" wrote:
Mike

Try

ddlGroup.Items.Clear()
ddlGroup.Items.Add("Select Company")
ddlGroup.AppendDataBoundItems = True

'The key is the appendDataBoundItems this will allow the added item to stay
when
the data source is bound to the dropdownlist it works best if you add the
item before the databinding so it is the first item.

'Create your datasource here
ddlGroup.DataSource = MyDatasource
ddlGroup.DataBind()
--
Jerry
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #4
This is the solution I used:

http://weblogs.asp.net/scottgu/archi...29/436804.aspx
"Mike" wrote:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The
dropdownlist values are successfully populated from a database table. I would
like to add the value "Select Company" which will be the selected value when
the page loads. I attempted to use the code below to add "Select Company"
dynamically but it is not added. Only the values from the database are in the
dropdownlist. I also tried to added it to the dropdownlist collection in
Visual Studio 2005 but "Select Company" was not added to the dropdownlist.
How do I add a value to the dropdownlist that is not in the database? Thank
you.

Dim NewItem As ListItem

NewItem = New ListItem("Select Company", "0")

DropDownList_companies.Items.Add(NewItem)
Sep 7 '06 #5
The first example using "AppendDataBoundItems" can be done in the Page_Load on a dropdownlist that uses SqlDataSource
Sep 19 '06 #6

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

Similar topics

2
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
1
by: strout | last post by:
Is it possible to add a dropdownlist to pager area so that user can select how many records to display on a page? Thank Strout
2
by: Manny Chohan | last post by:
I have a dropdown bound to database. I need to add Select one to the first index of drop down list. How can i do this? Thanks Manny
3
by: bmntech | last post by:
I want to create a client app, adding value to and building on my Web app. The user of this client app will be able to seamlessly access the public web site, but will also have additional...
9
by: Lammert | last post by:
Good morning, I create an ASP.NET 2.0 web application. The situation: 1. One masterpage where the users can select an organisation in a DropDownList. 2. Different content pages. I will get...
0
by: den 2005 | last post by:
Hi everybody, I created a Gridview with a TemplateField and there is Label control in ItemTemplate and a DropdownList control in EditItemTemplate, I was to displayed them ok when I click the...
0
by: Luqman | last post by:
Which event of Datagrid is fired when i click on a Datagrid New Row. I want to add a LineNo Value to its cell no. 5 ? I tried UserAddedRows Event but its not working ? How can I know, if the...
0
by: saiprex | last post by:
I have DropDownList control which is binded to SqlDataSource. The SqlDataSource gets the values from TABLE, it all works great. But how can i set the selected value for the DropDownList? The table is...
1
by: libish | last post by:
can we add a value to a list item?? i got a list with some list members here can we add a value to that list item??? ie. suppose a list contains items like "item1" "item2" "item3" etc...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.