473,654 Members | 3,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Selec t Company", "0")

DropDownList_co mpanies.Items.A dd(NewItem)

Sep 7 '06 #1
5 17165
Mike

Try

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

'The key is the appendDataBound Items 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.DataSo urce = MyDatasource
ddlGroup.DataBi nd()
--
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("Selec t Company", "0")

DropDownList_co mpanies.Items.A dd(NewItem)
Sep 7 '06 #2
Another way, besides Jerry's:

After the DataBining (you may bind the data all at once by calling
Page.DataBindin g() 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_co mpanies.DataSou rce=theSource
DropDownList_co mpanies.DataBin ding()

Dim NewItem As ListItem = New ListItem("Selec t Company", "0")
DropDownList_co mpanies.Items.I nsert(0, NewItem)
"Jerry C" <je*****@nospam .nospamwrote in message
news:89******** *************** ***********@mic rosoft.com...
Mike

Try

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

'The key is the appendDataBound Items 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.DataSo urce = MyDatasource
ddlGroup.DataBi nd()
--
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("Selec t Company", "0")

DropDownList_c ompanies.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:SqlDataSou rce) 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.Append DataBoundItems = True

'The key is the appendDataBound Items 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.DataSo urce = MyDatasource
ddlGroup.DataBi nd()
--
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("Selec t Company", "0")

DropDownList_co mpanies.Items.A dd(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("Selec t Company", "0")

DropDownList_co mpanies.Items.A dd(NewItem)
Sep 7 '06 #5
The first example using "AppendDataBoun dItems" 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
3745
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 <asp:EditCommandColumn> I want either a textbox or dropdown to appear when I check what is being edited. I thought I could create the dropdown on the fly and add it to the datagrid as below but It's not appearing. I imagined I could populate the dropdown
1
1561
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
1503
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
1228
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 functionality and content not available on the public site. The rational is to allow the user to work disconnected, to use the client's processing power, to remove the communications delay and to provide an additional layer of privacy.
9
3409
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 the value of the DropDownList in the Page_PreInit event. How can I do that? The content in the content and master pages is based on
0
2381
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 Edit Command button. But when I about to get the value of this template field I get an empty string value. I check the following the local dtFiles has data and the cells are visible at stage right before foreach GridViewRow gr in gdvFiles.Rows) loop....
0
989
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 current row is on AddMode or EditMode. If its on AddMode, then I will put the new LineNo in its cell otherwise not. Best Regards,
0
1840
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 a list of countries. Any help would be greatly appreciated, thanks
1
7235
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
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.