Dynamic Control creating in ItemDataBound

6 posts, 0 answers
  1. Reuven
    Reuven avatar
    19 posts
    Member since:
    Jun 2009

    Posted 13 Oct 2010 Link to this post

    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
  2. Reuven
    Reuven avatar
    19 posts
    Member since:
    Jun 2009

    Posted 13 Oct 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 18 Oct 2010 Link to this post

    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
  5. Reuven
    Reuven avatar
    19 posts
    Member since:
    Jun 2009

    Posted 18 Oct 2010 Link to this post

    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
  6. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 19 Oct 2010 Link to this post

    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
  7. Reuven
    Reuven avatar
    19 posts
    Member since:
    Jun 2009

    Posted 19 Oct 2010 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017