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

Item Templates And Adding New Items

3 Answers 125 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 16 Feb 2011, 11:35 PM
I have an ItemTemplate defined for my RadListBox. My list box is bound to the results of a Linq to SQL Query. In this case, it's bound to objects of type MyObject.

I'd like to be able to add a new RadListIBoxItem defined by the user. Ideally, I'd like to create a new MyObject instance and call myListBox.Items.Add(new RadListBoxItem(myNewObject)). This would then create a new item and bind the values in the ItemTemplate appropriately.

Of course, this isn't an option. I can only pass a text and value to the new RadListBoxItem, and this of course won't map to many of the controls in my ItemTemplate.

So what's the correct approach here? Is this simply the wrong control to use?

3 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 21 Feb 2011, 09:09 AM
Hi Robert,

In order to add templates dynamically, you should use ITemplate interface and its InstantiateIn method.
The general approach is the following, you "MyObject" class should implement the above mention interface and put your own implementation of the InstantiateIn method , which has input parameter , the container where it will be instantiated, the listbox item in your case. I've prepared a sample page showing how to implement this scenario.
I hope this would help you out.


Best wishes,
Dimitar Terziev
the Telerik team
0
Robert
Top achievements
Rank 1
answered on 21 Feb 2011, 04:18 PM
I'm not sure this gets me what I need.

The "MyObject" I refered to has several properties I need to display in the list box, not just a Text and a Value. So I can't simply rely on those two properties exposed by the RadListBoxItem.

I tried referencing the DateItem property inside my InstantiateIn method, but while this works initially, after any subsequent postbacks I'm left with an empty item again since the DataItem property doesn't survive the postbacks like the Text and Value properties do.

 

 

public void InstantiateIn(Control container)

 

 

{

 

 

 

    object dataItem = ((RadListBoxItem)container).DataItem;

 

 

 

 

    someControl.Text = (string)DataBinder.Eval(dataItem, "Property1");
    someOtherControl.Text = (string)DataBinder.Eval(dataItem, "Property2");
    anotherControl.Text = (string)DataBinder.Eval(dataItem, "Property3");

 

 

    container.Controls.Add(someControl);
    container.Controls.Add(someOtherControl);
    container.Controls.Add(anotherControl);

 

}

I was sure to assigned IDs to the controls I'm adding so that ViewState is preserved between postbacks, but I must be missing something.

Ideas?

0
Dimitar Terziev
Telerik team
answered on 22 Feb 2011, 04:57 PM
Hello Robert,

You are not limited only to the Text and Value of the listbox items, since you could bind numerous values to custom attributes. Every listbox item support custom attributes, which are name-value pairs, so try adding the necessary amount of custom attributes and bind values to them, as you are binding the text and value. This demo here demonstrates how you could bind data to custom attributes.

Greetings,
Dimitar Terziev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
ListBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Robert
Top achievements
Rank 1
Share this question
or