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

Discussion in 'ASP .Net Web Controls' started by Ron, Oct 6, 2004.

  1. Ron

    Ron Guest

    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
     
    Ron, Oct 6, 2004
    #1
    1. Advertising

  2. 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.)
     
    Steven Cheng[MSFT], Oct 7, 2004
    #2
    1. Advertising

  3. Ron

    Ron Guest

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

    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.

    "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.)
    >
    >
     
    Ron, Oct 7, 2004
    #3
  4. RE: ListItem VALUE -I can not set with code (taking text property

    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.)
     
    Steven Cheng[MSFT], Oct 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    2
    Views:
    495
    Mad Scientist Jr
    Jun 29, 2004
  2. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,592
    Ken Dopierala Jr.
    Oct 6, 2004
  3. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    527
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Nov 5, 2004
  4. =?Utf-8?B?QmVu?=
    Replies:
    2
    Views:
    1,972
    =?Utf-8?B?QmVu?=
    Feb 2, 2005
  5. Jim in Arizona
    Replies:
    7
    Views:
    973
    Badass Scotsman
    Nov 22, 2006
Loading...

Share This Page