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

DropdownList and DataValue

A.M
Hi,

I use following code on PageLoad to supply DataText for a drop down list
box:

Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then

Dim values as ArrayList= new ArrayList()

values.Add ("IN")
values.Add ("KS")
values.Add ("MD")
values.Add ("MI")
values.Add ("OR")
values.Add ("TN")

DropDown1.DataSource = values
DropDown1.DataBind
End If
End Sub

How can I change above code to spply both text and value to a listbox?

Thanks,
Ali
Nov 18 '05 #1
6 1922
Dim liItem As New ListItem

liItem.Value = 1

liItem.Text = "IN"

DropDown1.Items.Add(liItem)

Regards,

January Smith

"A.M" <no*****@online.nospam> wrote in message
news:O0**************@TK2MSFTNGP09.phx.gbl...
Hi,

I use following code on PageLoad to supply DataText for a drop down list
box:

Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then

Dim values as ArrayList= new ArrayList()

values.Add ("IN")
values.Add ("KS")
values.Add ("MD")
values.Add ("MI")
values.Add ("OR")
values.Add ("TN")

DropDown1.DataSource = values
DropDown1.DataBind
End If
End Sub

How can I change above code to spply both text and value to a listbox?

Thanks,
Ali

Nov 18 '05 #2
Hi Allan,

I agree with January's suggestion on using the ListItem as the DataSource
member, in addition to directory add ListItem object into the
DropDownList's Items collection, we can also use the DataBind mode to fill
the dropdownlist as below:

Dim items As New ListItemCollection

Dim i As Int32

For i = 1 To 10

Dim item As New ListItem
item.Text = "Text" + i.ToString()
item.Value = "Value" + i.ToString()

items.Add(item)
Next

lstMain.DataSource = items
lstMain.DataTextField = "Text"
lstMain.DataValueField = "Value"
lstMain.DataBind()

#lstMain is a DropDownList

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #3
A.M
Thank you Steven.

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:$%****************@cpmsftngxa10.phx.gbl...
Hi Allan,

I agree with January's suggestion on using the ListItem as the DataSource
member, in addition to directory add ListItem object into the
DropDownList's Items collection, we can also use the DataBind mode to fill
the dropdownlist as below:

Dim items As New ListItemCollection

Dim i As Int32

For i = 1 To 10

Dim item As New ListItem
item.Text = "Text" + i.ToString()
item.Value = "Value" + i.ToString()

items.Add(item)
Next

lstMain.DataSource = items
lstMain.DataTextField = "Text"
lstMain.DataValueField = "Value"
lstMain.DataBind()

#lstMain is a DropDownList

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #4
A.M
Sorry to reply a little late, But you created 10 ListItems (one in each
iteration)
Wouldn't it be more efficient to create just one ListItem, assign/change the
values and add it into listbox?

Thanks,
Alan
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:$%****************@cpmsftngxa10.phx.gbl...
Hi Allan,

I agree with January's suggestion on using the ListItem as the DataSource
member, in addition to directory add ListItem object into the
DropDownList's Items collection, we can also use the DataBind mode to fill
the dropdownlist as below:

Dim items As New ListItemCollection

Dim i As Int32

For i = 1 To 10

Dim item As New ListItem
item.Text = "Text" + i.ToString()
item.Value = "Value" + i.ToString()

items.Add(item)
Next

lstMain.DataSource = items
lstMain.DataTextField = "Text"
lstMain.DataValueField = "Value"
lstMain.DataBind()

#lstMain is a DropDownList

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5
Hi Alan,

Thanks for your advice. Yes, that'll be more effiecient. :)
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #6
Hi Alan,

I'm sorry to forget mention that the "create once I mean" is to declare the
the ListItem Reference once, and we still need to create multi-instance of
the ListItem when we need to add multi items into the DropDownList or other
such collection. Just like:

Dim i As Int32
Dim item As ListItem

For i = 1 To 10

item = New ListItem

item.Text = "Text" + i.ToString()
item.Value = "Value" + i.ToString()

items.Add(item)
Next

Otherwise, all the items will be all the same instances. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #7

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

Similar topics

0
by: Paul Owen | last post by:
Is there any way to set the item in a dropdownlist to correspond to a value from data? I have a number of dropdownlist controls within a detailview control. The detailview control represents 1...
1
by: Lars Pedersen | last post by:
Hi! Have can i select an item in a DropDown List, based on a datavalue. thanks -Lars
2
by: Hai Nguyen | last post by:
Hi everyone I'm developing an wep application, I got stuck into a problem. My database has a table which has 2 fields Table1 X Y A Apple B Banana C Cat
3
by: Newbie | last post by:
I need to populate a dropdownlist that will show two entries (title and billing rate) for each item. Example: Programmer $50 Engineer $50 .... Right now I'm only able to show the...
1
by: rodchar | last post by:
hey all, i have 2 fields that have delimited values in them. field1=1,2,3 field2=N1,N2,N3 what is the easiest way to get these into my dropdownlist where dropdownlist.datavalue = field 1...
2
by: Marc Robitaille | last post by:
Hello, I set the DataSource property of a DropDownList to as DataSet that is filled from a SQLDataAdapter. The AutoPostBack property of that DropDownList is set to True. When the...
6
by: Oscar | last post by:
I want to add items to a dropdownlist control within a Javascript eventhandler. This is what I code : var dd = document.getElementById("DropDownList1"); dd.Items.Add("1990");...
3
by: webmaster | last post by:
Pardon my not knowing where to find this one... but try finding the meaning of "this" on the net or in reference books. I'm trying to follow this example from Apress book - Introduction to C#...
2
by: teddymeu | last post by:
Hi Guys, this is kinda complicated but ill do my best to explain. I have two tables. products and categories. Products holds product info and an image, its primary key is ProductID. Category table...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.