Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - DropdownList and DataValue

 
Thread Tools Search this Thread
Old 05-21-2004, 06:43 PM   #1
Default DropdownList and DataValue


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




A.M
  Reply With Quote
Old 05-21-2004, 06:51 PM   #2
January Smith
 
Posts: n/a
Default Re: DropdownList and DataValue
Dim liItem As New ListItem

liItem.Value = 1

liItem.Text = "IN"

DropDown1.Items.Add(liItem)



Regards,

January Smith



"A.M" <> wrote in message
news:...
> 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
>
>





January Smith
  Reply With Quote
Old 05-22-2004, 02:07 AM   #3
Steven Cheng[MSFT]
 
Posts: n/a
Default Re: DropdownList and DataValue
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



Steven Cheng[MSFT]
  Reply With Quote
Old 05-23-2004, 05:38 AM   #4
A.M
 
Posts: n/a
Default Re: DropdownList and DataValue
Thank you Steven.

"Steven Cheng[MSFT]" <v-> wrote in message
news:$%...
> 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
>





A.M
  Reply With Quote
Old 05-25-2004, 04:43 PM   #5
A.M
 
Posts: n/a
Default Re: DropdownList and DataValue
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-> wrote in message
news:$%...
> 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
>





A.M
  Reply With Quote
Old 05-26-2004, 01:22 AM   #6
Steven Cheng[MSFT]
 
Posts: n/a
Default Re: DropdownList and DataValue
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



Steven Cheng[MSFT]
  Reply With Quote
Old 05-26-2004, 01:25 AM   #7
Steven Cheng[MSFT]
 
Posts: n/a
Default Re: DropdownList and DataValue
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



Steven Cheng[MSFT]
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46