Placing custom objects in RadListBox

7 posts, 1 answers
  1. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 10 May 2013 Link to this post

    We have a desktop application that is being ported to web, and while some of the code has to be redeveloped from the ground up, there is alot that won't need to be (I hope). In the desktop application we were able to populate comboboxes and listboxes with custom objects by simply overriding the ToString method.

    For example:
    Public Class SimpleItem
        Private _id As Integer
        Private _name As String
        Private _birthdate As Date
      
        Public Sub New(ByVal id As Integer, ByVal Name As String, ByVal Birthday As Date)
            _name = Name
            _id = id
            _birthdate = Birthday
        End Sub
      
        Public ReadOnly Property Age() As Integer
            Get
                Return If((Date.Today.Month >= _birthdate.Month) And (Date.Today.Day >= _birthdate.Day), Date.Today.Year - _birthdate.Year, Date.Today.Year - _birthdate.Year - 1)
            End Get
        End Property
      
        Public ReadOnly Property Name() As Integer
            Get
                Return _name
            End Get
        End Property
      
        Public Overrides Function ToString() As String
            Return String.Format("{0}, Age: {1}", _name, Me.Age)
        End Function
      
    End Class
      
        'Add several unique items to listbox
        MyListBox.Items.Add(New SimpleItem(12, "John Doe", New Date(1960, 12, 17)))
      
        'Loop through items
        For Each item As SimpleItem in MyListbox.Items
            If (item.Age >= 21)
                MsgBox (item.Name & " can purchase alcoholic beverages.")
            End If
        Next item


    Web applications however are a completely different thing, I can't add an object to the asp listbox control and I can't explicitly store an asp listboxitem into a class.

    The problem comes in when you start having larger classes with several properties that are not visible client-side because ToString sets what text of the control will be only, other items are never utilized and seemingly cannot be retrieved. We can set a value in the listbox, but if there are half a dozen proerties that need to make the round trip, it won't work.

    Winforms stores the object then sets the ToString value in the listbox and all class data is preserved. Is there something I am missing? Can this be done with Telerik Rad controls or will I need to completely rethink how to handle this issue? ToString and Value are simply not enough fields to work.
  2. Kate
    Admin
    Kate avatar
    1898 posts

    Posted 15 May 2013 Link to this post

    Hi Keith,

    Attached to this post you can find a very simplified runnable page that I believe would be helpful in implementing the desired functionality. I would also suggest that you refer to the following demo and help article where you can find more information on the different databindings that you can use with the control:
    Binding to ASP.NET Datasource components 
    ListBox DataSource

    Regards,
    Kate
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 15 May 2013 Link to this post

    After looking at the solution in the sample, that looks like exactly what I am needing. I just need to ensure that I can get the selected item as the object class .. i.e.

    CustomClass _item = ListBox1.SelectedItem;
  5. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 16 May 2013 Link to this post

    Ok, I have changed my custom class to work with the databinding to the ListBox, however, I am still at a loss on how to get access to the underlying object on postback.

    I am creating the EntityCollection<MyObject> and setting the Text and ID properties to the DataTextField and DataValueField. On postback, I need to be able to get the selected item from the data that was bound to the control, either that or I will have to do another call to the database.

    Any ideas?
  6. Kate
    Admin
    Kate avatar
    1898 posts

    Posted 20 May 2013 Link to this post

    Hi Keith,

    Here I attached an example how you can find a runnable page demonstrating how you can get the value of the selected item on postback of the page.

    Regards,
    Kate
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  7. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 20 May 2013 Link to this post

    I know how to get the value from the listbox selected item. That is a pretty simple thing to do. Actually it is all pretty simple to do, it is just some ways are more efficient than others.

    Currently, I am wondering what benefit I reap from binding the data to the listbox. If I end up having to capture the value of the selected item and then repopulate a datatable from the database, I may as well not use the overhead to bind the data in the first place ... unless I am missing big picture.

    Here is the procedure for the desktop app.
    capture GroupID from user
    get records from DB where GroupID match (14 fields retrieved from DB)
    create custom object from each row in the datatable
    populate listbox with the objects
    select item in listbox
    customObject = ListBox1.SelectedItem
    do stuff with customObject

    In the web application, I cannot fill a listbox with custom objects, so, I have to simply use the 'Text' and 'Value' fields to represent the objects.
    The scenario might be:
    capture GroupID from user
    get records from DB where GroupID match (return only GUID and Text fields)
    populate listbox, either through databinding or just adding the items one by one.
    select item in listbox
    get value from selected item
    retrieve all 14 fields from DB where GUID matches
    create custom object from the data returned from DB
    do stuff with custom object

    At least that is how I see it working out. Is there a better solution?
  8. Answer
    Kate
    Admin
    Kate avatar
    1898 posts

    Posted 23 May 2013 Link to this post

    Hi Keith,

    When you bind the RadListbox control to the datasource you do not keep reference to the object itself but rather only to the needed properties (Text, Value, ID or whatever you set). Thus, if you have initially bound the value of the item, you can later access it from the selected item. However, any other properties that are not bound, you should retrieve directly from your database.   

    Regards,
    Kate
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017