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

Multiple templates based on bound data

1 Answer 191 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 07 Aug 2012, 05:41 AM
Hey Guys,

I've come up against a sticky one here.  I'm trying to apply a dynamic (from UserControl) ItemTemplate to my RadListView.
I do this OnItemDataBound and it works great except for the fact the ItemTemplate seems to have already been set up and so I end up modifying the template ready for the next record and not the current one.  So all templates are staggered out-of-sync by one record.
It seems there is no event to handle that lets me check my dataitem in time to set the current ItemTemplate.  I;ve read a solution online regarding the vanilla .NET ListView where you create a custom version which exposes what is effectively a "OnItemCreating" event where you can query the underlying data item in time to set the ItemTemplate accordingly.

I guess my question is really, have I missed something in Telerik's ListView that will allow me to do this?  And if not, can I extend the Telerik ListView in the manner of that example and create an event where I can carry out my desired operations?

Many thanks for any help - code snippets below.

<telerik:RadListView ID="rlvJobsListPage" runat="server" ItemPlaceholderID="phJobsList" DataSourceId="objJobList"
    OnItemDataBound="rlvJobsListPage_ItemDataBound" AllowCustomPaging="true" AllowPaging="true" PageSize="20" AllowSorting="True">
    <LayoutTemplate>
        <div id="searchList">
            <div id="phJobsList" runat="server"></div>
        </div>
    </LayoutTemplate>
    <EmptyDataTemplate>
        No records found
    </EmptyDataTemplate>
    <ItemTemplate>First item gets this default - not my dynamic template like the rest of them do</ItemTemplate>            
</telerik:RadListView>

protected void rlvJobsListPage_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
{
    // Set each bound item to status "Loaded"
    if (e.Item is RadListViewDataItem)
    {
 
        RadListViewDataItem thisDataItem = (RadListViewDataItem)e.Item;
        Job thisListing = (Job)thisDataItem.DataItem;
 
        //Formulate the Template name and Render it!
        rlvJobsListPage.ItemTemplate = Templating.Render_ITemplate(Globals.SectionIds.Jobs, thisListing.ListingTemplate, TemplateType.Summary, this);           
         
 
    }
}

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 10 Aug 2012, 07:18 AM
Hi Simon,

I am posting the rpely from your ticket here in case anyone else is interested. If you have further questions please post them in the support thread.

There is no valid way to provide different ItemTemplates (in terms of structure) for RadListView. The control should use a single ItemTemplate and have it assigned not later than its Init phase. Furthermore, ItemDataBound does not fire on each postback, so any content you create during this event will be gone on the following postback that does not rebind the listview.
Your options are:

1) Keep the listview datasource in memory and initialize the items content during ItemCreated using the data from memory to determine which user control should be shown.
2) Load all content that could be shown in the listview item and hide the unneeded components (using Visible="false") during ItemDataBound when you already have the data.


And an addition to 1), even when using ItemCreated you cannot have the approach that assigns the ItemTemplate of the RadListView in an item event. You should rather use one ItemTemplate only, with a PlaceHolder in it and based on the data, load different user controls inside it.

Kind regards,
Tsvetina
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.
Tags
ListView
Asked by
Simon
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or