ListItem VALUE -I can not set with code (taking text property inst

ListItem VALUE -I can not set with code (taking text property inst

Post by Um9u » Thu, 07 Oct 2004 23:51:04


I am trying to use code to create a dropdownlist box that has different
values than text for instance:

<asp:DropDownList id="someID" runat="server">
<asp:listitem value="12345">ABCDE</asp:listitem>
<asp:listitem value="98765">FGHIJ</asp:listitem>
</asp:DropDownList>

I am using code similar to the following:
----------------------------------------------------
dim ddListItemCollection as New ListItemCollection
dim i as integer
dim strI as string

for i = 0 to 3
strI = "Some string based on i"
ddListItemCollection.Add(New ListItem(strI,i))
next
------------------------------------------------------------
This should render HTML something like

<select name="SomeName" id="SomeID">
<option value="0">Some string based on 0</option>
<option value="1">Some string based on 1</option>
<option value="2">Some string based on 2</option>
<option value="3">Some string based on 3</option>
</select>

But it is coming out like this:

<select name="SomeName" id="SomeID">
<option value="Some string based on 0">Some string based on 0</option>
<option value="Some string based on 1">Some string based on 1</option>
<option value="Some string based on 2">Some string based on 2</option>
<option value="Some string based on 3">Some string based on 3</option>
</select>

where the Value attribute is taking it's value from the text property.

I need to set the value as something different than the text. Any help you
all can offer will be appreciated.

Thanks,

Ron
 
 
 

ListItem VALUE -I can not set with code (taking text property inst

Post by v-schan » Fri, 08 Oct 2004 11:38:32

Hi Ron,

As for the DropDownList adding items problem, I've also done some tests ,it
seems that the ListItem's
[Visual Basic]
Public Sub New( _
ByVal text As String, _
ByVal value As String _
)

constructor can works as long as we set the text and value with the correct
string. Here is the test code I used:
#phMain is a placeholder on the page
====================
Dim lst As New System.Web.UI.WebControls.DropDownList

lst.ID = "lstValues"

Dim item As ListItem
For i = 1 To 5

lst.Items.Add(New ListItem("text_" + i.ToString(), i))

Next

phMain.Controls.Add(lst)
===========================

In addition, we can also use databinding to add items into dropdownlist,
for example:
#listMain is a dropdownlist on the page
==========================
Private Sub Bind_Data()

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()

End Sub
======================

You may have a try on both. If there is still anything unclear, please feel
free to post here. 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.)