This is a migrated thread and some comments may be shown as answers.

Placing custom objects in RadListBox

6 Answers 126 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Keith
Top achievements
Rank 2
Keith asked on 11 May 2013, 04:32 AM
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.

6 Answers, 1 is accepted

Sort by
0
Kate
Telerik team
answered on 15 May 2013, 03:05 PM
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.
0
Keith
Top achievements
Rank 2
answered on 15 May 2013, 05:17 PM
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;
0
Keith
Top achievements
Rank 2
answered on 16 May 2013, 06:17 AM
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?
0
Kate
Telerik team
answered on 20 May 2013, 02:32 PM
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.
0
Keith
Top achievements
Rank 2
answered on 20 May 2013, 07:15 PM
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?
0
Accepted
Kate
Telerik team
answered on 23 May 2013, 01:51 PM
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.
Tags
ListBox
Asked by
Keith
Top achievements
Rank 2
Answers by
Kate
Telerik team
Keith
Top achievements
Rank 2
Share this question
or