Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 31st, 2003, 05:33 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default listitem

i do the to scenarios but one only work
all i do is i populate the dropdownlist control with some rows retrieved from a Database table(only two fields returned),BUT
the problem seam to be with ListItem Class but why?

the follows are the two scenarios

scenario1:
dim listItem1 as new listitem
 while datareader1.read
   listItem1.value=datareader1("ID")
   listItem1.Text=datareader1("Name")
   DropDownList.items.add(listItem1)
 loop

scenario2:
 while datareader1.read
   dim listItem1 as new listitem
   listItem1.value=datareader1("ID")
   listItem1.Text=datareader1("Name")
   DropDownList.items.add(listItem1)
 loop


but only scenario2 is the working one, do any one have an explaination









Ahmed Ali
Software Developer
__________________
Ahmed Ali
Senior Software Developer
 
Old January 1st, 2004, 09:52 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you have a declared object like your "listitem1" and you add this to another control, whether you add it to the other control's "controls" collection or an "items" collection the reference to the declared object doesn't go away. It doesn't "add and forget" the original object. If you take that same object and ".Add(...)" it to a different control, it leaves the first. If the first happens to be the same as the second, like in your scenario, it's still there, but it's not duplicated. I think this is due to the way the objects store references to other objects. You need to create a new instance of the control each time you need a new one.

One better way to do what you are doing is:
dim listItem1 as listitem
while datareader1.read
    listItem1 = new listitem
    listItem1.value=datareader1("ID")
    listItem1.Text=datareader1("Name")
    DropDownList.items.add(listItem1)
loop

Alternatively you could do this:
dim listItem1 as listitem
while datareader1.read
    listItem1 = new listitem(datareader1("Name"), datareader1("ID"))
    DropDownList.items.add(listItem1)
loop

Or:
while datareader1.read
    DropDownList.items.add(new listitem(datareader1("Name"), datareader1("ID")))
loop

My recommendation is to drop the whole loop altogether and utilize the databinding capabilities of the control:
DropDownList.DataSource = datareader1
DropDownList.DataTextField = "Name"
DropDownList.DataValueField = "ID"
DropDownList.DataBind()

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 3rd, 2004, 10:13 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

txs Peter the last Databinding Sol. is the best

Ahmed Ali
Software Developer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing ListItem objects Andrew.Berry ASP.NET 2.0 Professional 1 April 8th, 2008 12:25 PM
set tooltiptext on every listitem for Combo myself.panku ASP.NET 1.0 and 1.1 Professional 0 March 18th, 2008 08:57 AM
SelectedIndexChange uses ListItem value Randy76 VS.NET 2002/2003 3 September 11th, 2003 09:33 AM
ListView, ListItem problems/questions Tremmorkeep C# 0 July 17th, 2003 04:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.