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

Problem with binding ListItemCollection with DropDownList

P: n/a
Hi

There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values are different

i.e.
ListItem[0].Text = "Name0"
ListItem[0].Value = "ID0"

ListItem[1].Text = "Name1"
ListItem[1].Value = "ID1"

The above ListItem(s) are a part of the ListItemCollection that I mentioned before

Then I set the DataSource of a DropDownList (on my page) to the ListItemCollection and bind it

DropDownList.DataSource = ListItemCollection
DropDownList.DataBind()

But the DropDownList does not pick up the Value and the Text properties separately. It sets the Text Property of each ListItem as the Value also

On the other hand if I iterate through the ListItemCollection and Add() each ListItem to the DropDownList, then the Value and Text fields are picked separately

i.e.

foreach(ListItem in ListItemCollection

DropDownLIst.Items.Add(ListItem)
Can someone tell me why this is happening and if this is some sort of known issue in .NET

RAMADU
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi RAMADU,

Is there any specificy reason for you to use the ListItemCollection ???

you can easily solve the problem using a simply hashtable as follows.

put the code in the page_load

Hashtable myHashtable = new Hashtable();
myHashtable .Add("One","One");
myHashtable .Add("Two","Two");
myHashtable .Add("Three","Three");

DropDownList1.DataSource = myHashtable ;
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();

Regards
Ashish M Bhonkiya

"RAMADU" <sr******************@patni.com> wrote in message
news:EB**********************************@microsof t.com...
Hi,

There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values
are different.
i.e.
ListItem[0].Text = "Name0";
ListItem[0].Value = "ID0";

ListItem[1].Text = "Name1";
ListItem[1].Value = "ID1";

The above ListItem(s) are a part of the ListItemCollection that I mentioned before.
Then I set the DataSource of a DropDownList (on my page) to the ListItemCollection and bind it.
DropDownList.DataSource = ListItemCollection;
DropDownList.DataBind();

But the DropDownList does not pick up the Value and the Text properties separately. It sets the Text Property of each ListItem as the Value also.
On the other hand if I iterate through the ListItemCollection and Add() each ListItem to the DropDownList, then the Value and Text fields are picked
separately.
i.e.

foreach(ListItem in ListItemCollection)
{
DropDownLIst.Items.Add(ListItem);
}

Can someone tell me why this is happening and if this is some sort of known issue in .NET?
RAMADU

Nov 18 '05 #2

P: n/a
Hi Ramadu,
You can can even do it using the ListItemCollection .

You need to provide the DataTextField and DataValueField before you do the
Call the Databind method or the DropDownList

Here is a sample code to explain how to do this.

Put this code on the page_load

ListItemCollection myColl = new ListItemCollection();

myColl.Add( new ListItem("One","One"));
myColl.Add( new ListItem("Two","Two"));
myColl.Add( new ListItem("Three","Three"));
DropDownList1.DataSource = myColl;
// This two lines you are missing ???
DropDownList1.DataTextField = "text";
DropDownList1.DataValueField = "value";
DropDownList1.DataBind();

HTH
Regards
Ashish M Bhonkiya

"RAMADU" <sr******************@patni.com> wrote in message
news:EB**********************************@microsof t.com...
Hi,

There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values
are different.
i.e.
ListItem[0].Text = "Name0";
ListItem[0].Value = "ID0";

ListItem[1].Text = "Name1";
ListItem[1].Value = "ID1";

The above ListItem(s) are a part of the ListItemCollection that I mentioned before.
Then I set the DataSource of a DropDownList (on my page) to the ListItemCollection and bind it.
DropDownList.DataSource = ListItemCollection;
DropDownList.DataBind();

But the DropDownList does not pick up the Value and the Text properties separately. It sets the Text Property of each ListItem as the Value also.
On the other hand if I iterate through the ListItemCollection and Add() each ListItem to the DropDownList, then the Value and Text fields are picked
separately.
i.e.

foreach(ListItem in ListItemCollection)
{
DropDownLIst.Items.Add(ListItem);
}

Can someone tell me why this is happening and if this is some sort of known issue in .NET?
RAMADU

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.