By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

Adding Value to Dropdownlist

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
The first example using "AppendDataBoundItems" can be done in the Page_Load on a dropdownlist that uses SqlDataSource
Sep 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.