databinding a Dropdownlist to another dropdownlist

T

tshad

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=DataValueField
ActualExperienceLevel.databind()
ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
ActualExperienceLevel.Items(0).value = 0

But that doesn't seem to work.

Thanks,

Tom
 
B

bcutting

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
 
G

Guest

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.Items
ActualExperienceLevel.DataBind()

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

Tim
 
T

tshad

timkling said:
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.Items
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
 
B

Bruce Barker

a datareader can not be used twice, because it forward only cursor. the
second databind will find itself at the end.

-- bruce (sqlwork.com)
 
G

Guest

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
 
T

tshad

timkling said:
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
 
T

tshad

tshad said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top