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

Dynamic Control creating in ItemDataBound

5 Answers 278 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Reuven
Top achievements
Rank 1
Reuven asked on 13 Oct 2010, 08:52 AM
I have a scenario where depending on the the values in a field of itemdata, I need to generate a different control. Obviously in ItemCreated the dataitem is not bound yet, but strangely if I add controls in ItemDataBound, they disappear by the time the control is rendered. For example:
If TypeOf (e.Item) Is RadListViewDataItem Then
    Dim FormItem As MyFormItem = CType(CType(e.Item, RadListViewDataItem).DataItem, MyFormItem)
    Select Case FormItem.TypeId
        Case "Header"
            Dim lbl As New Label
            lbl.Text = String.Format("<b>{0}</b>", FormItem.Text)
            e.Item.Controls.Add(lbl)
there the ItemTemplate is initially empty.

Is this possible?

Thanks,
Reuven

5 Answers, 1 is accepted

Sort by
0
Reuven
Top achievements
Rank 1
answered on 13 Oct 2010, 11:07 AM
Figured out that the problem is actually the stage of the control lifecycle, so if I generate a postback, it gets updated with the right control structure, not sure how to overcome this
0
Veli
Telerik team
answered on 18 Oct 2010, 09:52 AM
Hello Reuven,

ItemDataBound is not necessarily fired on every postback, so it is a bad candidate for instantiating dynamic controls in. Particularly, ItemDataBound fires when RadListView rebinds. So, it will always fire on initial load, but it will not fire on postback unless RadListView is rebound on postback.

Ideally, you would want to create your items in ItemCreated and ItemDataBound. That is, if you created your items in ItemDataBound, you need to remember that and recreate the same controls in ItemCreated  so that ViewState can properly be restored to them.

Regards,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Reuven
Top achievements
Rank 1
answered on 18 Oct 2010, 11:46 AM
Thank you for the reply. The problem is that the dataitem is not available at ItemCreated - I want each item to have a different set of controls depending on the bound item (ie, if the item is a "header" to render a textbox, but if it's a "checkbox" to render a checkbox, etc - it is a dynamic form generator)

thanks,
Reuven
0
Veli
Telerik team
answered on 19 Oct 2010, 09:42 AM
Hi Reuven,

Here is an approach you can take:

1. Create your items dynamically in ItemDataBound on initial load.
2. Save the data from the e.Item.DataItem object that identifies which controls are created dynamically.
3. Use ItemCreated on postback based on the saved data from step2
4. If the ListView is rebound, start over from step 1.

Using the above approach, you use both ItemCreated and ItemDataBound to dynamically created the controls in the listview items. But you need to save the data item's values that help you identify what controls should be recreated on postback. You then use this info in ItemCreated to re-recreate the controls. If RadListView is rebound, your dynamic items created in ItemCreated will be gone anyway, so you start over from ItemDataBound again.

Does it make sense?

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Reuven
Top achievements
Rank 1
answered on 19 Oct 2010, 07:01 PM
absolutely - that's what I wound up doing, but thought it wasn't the most efficient way. Am now just having weird AJAX issues but that's for a different forum ;-)

Thanks,
Reuven
Tags
ListView
Asked by
Reuven
Top achievements
Rank 1
Answers by
Reuven
Top achievements
Rank 1
Veli
Telerik team
Share this question
or