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

databinding a Dropdownlist to another dropdownlist

Can you databind a dropdownlist to another dropdownlist?

I have 2 identical list. I am getting my data from a DataReader, so as soon
as I have bound the first DDL, I can't do it again to the next as the
dataReader can only be read once.

I tried:

ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.databind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel
ActualExperienceLevel.DataTextField= DataTextField
ActualExperienceLevel.DataValueField=DataValueFiel d
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom
Nov 19 '05 #1
8 1654
Fill an ArrayList with DictionaryEntries, then bind to that.

Example
ArrayList arlItems = new ArrayList ();
while (dReader.Read ())
arlItem.Add (new DictionaryEntry (dReader["Description"].ToString (),
dReader["ExperienceLevelID].ToString());

then create an array and set the datasources

ActualExperienceLevel.DataSource = arrDEntries;
ActualExperienceLevel.DataTextField = "value";
ActualExperienceLevel.DataValueField = "key";

Hope this helps,
Brendan

Nov 19 '05 #2
To bind to a dropdownlist the datasource must implement IListSource or
IEnumerable. DropDownList.Items is enumerable.

So this will work:
ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.DataBind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel.I tems
ActualExperienceLevel.DataBind()

Don't add your default item because it is already in the list.

Tim

"tshad" wrote:
Can you databind a dropdownlist to another dropdownlist?

I have 2 identical list. I am getting my data from a DataReader, so as soon
as I have bound the first DDL, I can't do it again to the next as the
dataReader can only be read once.

I tried:

ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.databind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel
ActualExperienceLevel.DataTextField= DataTextField
ActualExperienceLevel.DataValueField=DataValueFiel d
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom

Nov 19 '05 #3
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
To bind to a dropdownlist the datasource must implement IListSource or
IEnumerable. DropDownList.Items is enumerable.

So this will work:
ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.DataBind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel.I tems
ActualExperienceLevel.DataBind()

Worked great.
Don't add your default item because it is already in the list.
Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level")
Thanks,

Tom
Tim

"tshad" wrote:
Can you databind a dropdownlist to another dropdownlist?

I have 2 identical list. I am getting my data from a DataReader, so as
soon
as I have bound the first DDL, I can't do it again to the next as the
dataReader can only be read once.

I tried:

ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.databind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel
ActualExperienceLevel.DataTextField= DataTextField
ActualExperienceLevel.DataValueField=DataValueFiel d
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom

Nov 19 '05 #4
a datareader can not be used twice, because it forward only cursor. the
second databind will find itself at the end.

-- bruce (sqlwork.com)
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OY**************@TK2MSFTNGP14.phx.gbl...
Can you databind a dropdownlist to another dropdownlist?

I have 2 identical list. I am getting my data from a DataReader, so as
soon as I have bound the first DDL, I can't do it again to the next as the
dataReader can only be read once.

I tried:

ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.databind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel
ActualExperienceLevel.DataTextField= DataTextField
ActualExperienceLevel.DataValueField=DataValueFiel d
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom

Nov 19 '05 #5
> Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level") Yes. Do it to the first list, but after that it will be in the second list
automatically.
Tim

"tshad" wrote:
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
To bind to a dropdownlist the datasource must implement IListSource or
IEnumerable. DropDownList.Items is enumerable.

So this will work:
ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.DataBind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel.I tems
ActualExperienceLevel.DataBind()


Worked great.
Don't add your default item because it is already in the list.


Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level")
Thanks,

Tom

Tim

"tshad" wrote:
Can you databind a dropdownlist to another dropdownlist?

I have 2 identical list. I am getting my data from a DataReader, so as
soon
as I have bound the first DDL, I can't do it again to the next as the
dataReader can only be read once.

I tried:

ExperienceLevel.DataSource=dbReader
ExperienceLevel.DataTextField= "Description"
ExperienceLevel.DataValueField="ExperienceLevelID"
ExperienceLevel.databind()
ExperienceLevel.Items.Insert(0, "Select Experience Level")
ExperienceLevel.Items(0).value = 0

ActualExperienceLevel.DataSource=ExperienceLevel
ActualExperienceLevel.DataTextField= DataTextField
ActualExperienceLevel.DataValueField=DataValueFiel d
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom


Nov 19 '05 #6
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level") Yes. Do it to the first list, but after that it will be in the second
list
automatically.


Worked as advertised.

Thanks,

Tom
Tim

"tshad" wrote:
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
> To bind to a dropdownlist the datasource must implement IListSource or
> IEnumerable. DropDownList.Items is enumerable.
>
> So this will work:
> ExperienceLevel.DataSource=dbReader
> ExperienceLevel.DataTextField= "Description"
> ExperienceLevel.DataValueField="ExperienceLevelID"
> ExperienceLevel.DataBind()
> ExperienceLevel.Items.Insert(0, "Select Experience Level")
> ExperienceLevel.Items(0).value = 0
>
> ActualExperienceLevel.DataSource=ExperienceLevel.I tems
> ActualExperienceLevel.DataBind()
>


Worked great.
> Don't add your default item because it is already in the list.


Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level")
Thanks,

Tom
>
> Tim
>
> "tshad" wrote:
>
>> Can you databind a dropdownlist to another dropdownlist?
>>
>> I have 2 identical list. I am getting my data from a DataReader, so as
>> soon
>> as I have bound the first DDL, I can't do it again to the next as the
>> dataReader can only be read once.
>>
>> I tried:
>>
>> ExperienceLevel.DataSource=dbReader
>> ExperienceLevel.DataTextField= "Description"
>> ExperienceLevel.DataValueField="ExperienceLevelID"
>> ExperienceLevel.databind()
>> ExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ExperienceLevel.Items(0).value = 0
>>
>> ActualExperienceLevel.DataSource=ExperienceLevel
>> ActualExperienceLevel.DataTextField= DataTextField
>> ActualExperienceLevel.DataValueField=DataValueFiel d
>> ActualExperienceLevel.databind()
>> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ActualExperienceLevel.Items(0).value = 0
>>
>> But that doesn't seem to work.
>>
>> Thanks,
>>
>> Tom
>>
>>
>>


Nov 19 '05 #7

"timkling" <ti******@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level") Yes. Do it to the first list, but after that it will be in the second
list
automatically.
Tim


Just realized that it wasn't doing it correctly.

I have the following:

CopyResumeTo1.DataSource = dbReader
CopyResumeTo1.DataValueField = "UserID"
CopyResumeTo1.DataTextField = "FullName"
CopyResumeTo1.DataBind()
CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))

CopyResumeTo2.DataSource=CopyResumeTo1.Items
CopyResumeTo2.DataBind()

CopyResumeTo3.DataSource=CopyResumeTo1.Items
CopyResumeTo3.DataBind()

The first one (CopyResumeTo1) is working correctly. It has the
DataValueField (UserID) as an integer and the DataTextFiled (FullName) as
the name and if I look at the ViewSource, it is correct.

But when I look at the other 2 dropdowns, it is putting the FullName in both
the DataValueField and the DataTextField.

Also, I thought my insert would put a 0 in the Value field, but it put a
blank there. Is my format incorrect?

CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0")

Thanks,

Tom
"tshad" wrote:
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
> To bind to a dropdownlist the datasource must implement IListSource or
> IEnumerable. DropDownList.Items is enumerable.
>
> So this will work:
> ExperienceLevel.DataSource=dbReader
> ExperienceLevel.DataTextField= "Description"
> ExperienceLevel.DataValueField="ExperienceLevelID"
> ExperienceLevel.DataBind()
> ExperienceLevel.Items.Insert(0, "Select Experience Level")
> ExperienceLevel.Items(0).value = 0
>
> ActualExperienceLevel.DataSource=ExperienceLevel.I tems
> ActualExperienceLevel.DataBind()
>


Worked great.
> Don't add your default item because it is already in the list.


Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level")
Thanks,

Tom
>
> Tim
>
> "tshad" wrote:
>
>> Can you databind a dropdownlist to another dropdownlist?
>>
>> I have 2 identical list. I am getting my data from a DataReader, so as
>> soon
>> as I have bound the first DDL, I can't do it again to the next as the
>> dataReader can only be read once.
>>
>> I tried:
>>
>> ExperienceLevel.DataSource=dbReader
>> ExperienceLevel.DataTextField= "Description"
>> ExperienceLevel.DataValueField="ExperienceLevelID"
>> ExperienceLevel.databind()
>> ExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ExperienceLevel.Items(0).value = 0
>>
>> ActualExperienceLevel.DataSource=ExperienceLevel
>> ActualExperienceLevel.DataTextField= DataTextField
>> ActualExperienceLevel.DataValueField=DataValueFiel d
>> ActualExperienceLevel.databind()
>> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ActualExperienceLevel.Items(0).value = 0
>>
>> But that doesn't seem to work.
>>
>> Thanks,
>>
>> Tom
>>
>>
>>


Nov 19 '05 #8
"tshad" <ts**********@ftsolutions.com> wrote in message
news:u$**************@TK2MSFTNGP14.phx.gbl...

"timkling" <ti******@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level") Yes. Do it to the first list, but after that it will be in the second
list
automatically.
Tim


Just realized that it wasn't doing it correctly.

I have the following:

CopyResumeTo1.DataSource = dbReader
CopyResumeTo1.DataValueField = "UserID"
CopyResumeTo1.DataTextField = "FullName"
CopyResumeTo1.DataBind()
CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))

CopyResumeTo2.DataSource=CopyResumeTo1.Items
CopyResumeTo2.DataBind()

CopyResumeTo3.DataSource=CopyResumeTo1.Items
CopyResumeTo3.DataBind()


I found what needed to be added to make this work.

You also need the DataValueField and DataTextField defined, but refering to
the properties of the ListItem and not the original DataReader. So the
above needs to be done as so:

CopyResumeTo1.DataSource = dbReader
CopyResumeTo1.DataValueField = "UserID"
CopyResumeTo1.DataTextField = "FullName"
CopyResumeTo1.DataBind()
CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))

CopyResumeTo2.DataSource=CopyResumeTo1.Items
CopyResumeTo2.DataValueField = "Value"
CopyResumeTo2.DataTextField = "Text"
CopyResumeTo2.DataBind()

CopyResumeTo3.DataSource=CopyResumeTo1.Items
CopyResumeTo3.DataValueField = "Value"
CopyResumeTo3.DataTextField = "Text"
CopyResumeTo3.DataBind()

In DataReader you use the Column name as the source, if you use the
ListItem - there is no property "UserID" or "FullName". There are 3
properties - "Selected", "Value" and "Text". In this case, the Value and
Text properties.

The first one (CopyResumeTo1) is working correctly. It has the
DataValueField (UserID) as an integer and the DataTextFiled (FullName) as
the name and if I look at the ViewSource, it is correct.

But when I look at the other 2 dropdowns, it is putting the FullName in
both the DataValueField and the DataTextField.

Also, I thought my insert would put a 0 in the Value field, but it put a
blank there. Is my format incorrect?
Actually, this format was correct, but I was doing a refresh to check it and
I was not re-loading the dropdowns, once I did, it worked fine.

Tom

CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0")

Thanks,

Tom

"tshad" wrote:
"timkling" <ti******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
> To bind to a dropdownlist the datasource must implement IListSource or
> IEnumerable. DropDownList.Items is enumerable.
>
> So this will work:
> ExperienceLevel.DataSource=dbReader
> ExperienceLevel.DataTextField= "Description"
> ExperienceLevel.DataValueField="ExperienceLevelID"
> ExperienceLevel.DataBind()
> ExperienceLevel.Items.Insert(0, "Select Experience Level")
> ExperienceLevel.Items(0).value = 0
>
> ActualExperienceLevel.DataSource=ExperienceLevel.I tems
> ActualExperienceLevel.DataBind()
>

Worked great.

> Don't add your default item because it is already in the list.

Which default item - the insert?

ExperienceLevel.Items.Insert(0, "Select Experience Level")
Thanks,

Tom
>
> Tim
>
> "tshad" wrote:
>
>> Can you databind a dropdownlist to another dropdownlist?
>>
>> I have 2 identical list. I am getting my data from a DataReader, so
>> as
>> soon
>> as I have bound the first DDL, I can't do it again to the next as the
>> dataReader can only be read once.
>>
>> I tried:
>>
>> ExperienceLevel.DataSource=dbReader
>> ExperienceLevel.DataTextField= "Description"
>> ExperienceLevel.DataValueField="ExperienceLevelID"
>> ExperienceLevel.databind()
>> ExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ExperienceLevel.Items(0).value = 0
>>
>> ActualExperienceLevel.DataSource=ExperienceLevel
>> ActualExperienceLevel.DataTextField= DataTextField
>> ActualExperienceLevel.DataValueField=DataValueFiel d
>> ActualExperienceLevel.databind()
>> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
>> ActualExperienceLevel.Items(0).value = 0
>>
>> But that doesn't seem to work.
>>
>> Thanks,
>>
>> Tom
>>
>>
>>


Nov 19 '05 #9

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

Similar topics

2
by: Leon Shaw | last post by:
have two Dropdownlist and one which is containing States and one containing Cities. I know how to do all the little databinding task in visual studios interface as well as set the relationship...
4
by: JV | last post by:
It's easy to databind a listbox or dropdownlist if all you want is to fill it with a list of values. There are plenty of examples in the online help. Unfortunately, real world applications...
1
by: Joe Gass | last post by:
I'd like to bind some xml to a dropdownlist <engines> <engine name="test1" id="1" /> <engine name="test2" id="2" /> </engines> If I do: ddlEngines.DataSource =...
2
by: Nathan Sokalski | last post by:
I have several DropDownList controls on my page that use databinding. However, I want to give users the option of selecting a choice such as "None Selected" or something else that shows they did...
2
by: Dave A | last post by:
I am stuggling with databinding a drop down list, hooking into the SelectedIndexChanged and attempting to avoid using the viewstate. The drop down list is quite large so I would prefer to avoid...
0
by: Simon Gregory | last post by:
I am currently attempting to figure out how the new databinding stucture works in ASP.NET 2.0 after working with v1.0 & v1.1 for several years. It seems that if you wish to do set up databinding...
0
by: moi | last post by:
Hello, With ASP.NET 2 , i have two dropdownlist, one "A" (not in a formview) in the web page and other one "B" in a Insert Template in a formview. There's a link (parameter between this two...
1
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. ...
0
by: =?Utf-8?B?cGhpbGptY2c=?= | last post by:
I've seen this question asked 100 times, but with no reasonable answer, other than to extend a control etc. Sorry if I'm going over old ground. I've a DropDownList inside a FormView control,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.