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

Problem with binding ListItemCollection with DropDownList

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
2 12770
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
2
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
2
by: Josema | last post by:
Hi, I have a DropDownList called BSDropDown and the following code: this.BSDropDown.DataSource=B2BSS_; this.BSDropDown.DataTextField="Name"; this.BSDropDown.DataValueField="ID"; ListItem...
0
by: Goncalo Faria | last post by:
Hi. I'll go directly to my problem: I have a repeater that is binded with data. For each data row I want the user to be able to change a field, that is represented by a DropDownList. My...
4
by: Jenny C. | last post by:
Hi I have a DropDownList in a ASP.NET (C#) page. This control is bound to a DataTable to get the values to display However, the SelectedItem never seems to change For example, if I change...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
1
by: 47computers | last post by:
Hello. I'm having a bit of difficulty with some DropDownList controls within templates in a DetailsView on my web form. Basically, I have a form where a user is building a "location profile" with...
0
by: 47computers | last post by:
Hello. I'm having a bit of difficulty with some DropDownList controls within templates in a DetailsView on my web form. Basically, I have a form where a user is building a "location profile" with...
1
by: JJ | last post by:
Hi. I am having trouble getting a dropdownlist to work properly in a detailsview: The code is something like: <asp:DetailsView ID="dvwSubscriber" runat="server" AutoGenerateRows="False" ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...
0
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...

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.