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

R

Ron

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
 
S

Steven Cheng[MSFT]

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.)
 
R

Ron

Thanks this helped a lot. Just FYI for the readers, I was binding the data
on the HTML side and that is where I was making the mistakes. With your
info, I figured this out and it worked fine. Creating the ListItemCollection
the same way you did in sub Bind_Data(), I then binded the collection to the
DropDownList inside the tag like this:

<asp:DropDownList
ID="SomeID"
Runat="server"
DataSource="<%# items %>"
DataValueField='<%# "Value" %>'
DataTextField='<%# "Text" %>'>
</asp:DropDownList>

Before, I did not have the DataValueField or DataTextField attributes set.
If anyone uses this DropDownList Example, they should note the single quotes
used with DataValueField and DataTextField (and the double quotes around the
Value and Text fields). I guess I did not realize that 'Value' and 'Text'
were "Field Names" of the ListItem.

Thanks again, this was a big help.
 
S

Steven Cheng[MSFT]

Hi Ron,

You're welcome. I'm also glad that my suggestions has been of assistance.
Thanks again for posting and please always feel free to post here when you
need any help on asp.net:).

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top