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.
[quoted text, click to view] "Steven Cheng[MSFT]" wrote:
> 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.)
>