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

Add user controls to RadListView programmatically

6 Answers 419 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Amy
Top achievements
Rank 1
Amy asked on 20 Jun 2011, 04:25 PM
Hi.  Could someone point me to or show me an example of how to add user controls to a RadListView in code behind please. 

I have defined the RadListView in the aspx page.  In the code behind I create a DataTable containing ID numbers.  Next, I loop through these numbers and for each one instantiate two user controls, rebind the user controls, and then add them to the RadListView.

I have the logic to do all of this except I cannot seem to figure out how to add the user controls to the RadListView.  No editing/changes will be made to the user controls, I just need to display a horizontal list of the controls for informational purposes.

Here's the aspx code:
<asp:Panel ID="rpItem" runat="server" Scrolling="Y">
<telerik:RadListView ID="RadListView1" runat="server" DataKeyNames="iItemID, iSecID"
        ItemPlaceholderID="ItemPlaceHolder1" AllowPaging="true" Skin="Windows7"
        PageSize="1" Height="325px" >
        <LayoutTemplate>
            <asp:Panel ID="ItemPlaceHolder1" runat="server" Height="300px">
            </asp:Panel>
        </LayoutTemplate>
        <ItemTemplate>
             
        </ItemTemplate>
    </telerik:RadListView>
</asp:Panel>

Here's the code behind:
Dim tblItemIDs As DataTable = GetItemIDs()
 
For Each r As DataRow In tblItemIDs.Rows
 
    Me.hfItem.Value = r("iItemID")
 
    Dim ucItemInfo As UserControl = Page.LoadControl("/controls/iItem_Info.ascx")
    Dim infoView As RadListView = DirectCast(ucItemInfo.FindControl("RadListView1"), RadListView)
    infoView.Rebind()
 
    'Need to add the ucItemInfo  user control
 
  Dim ucItemPreview As UserControl = Page.LoadControl("/controls/iItem_Preview.ascx")
  Dim listView As RadListView = DirectCast(ucItemPreview.FindControl("RadListView1"), RadListView)
    listView.Rebind()
 
  'Need to add the ucItemPreview user control
 
  RadListView1.DataBind()
 
Next


Any help would be greatly appreciated.
Thanks!

6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 23 Jun 2011, 08:16 AM
Hello Amy,

If you need to dynamically initialize user controls in RadListView, you should also dynamically create RadListView in the Init phase of the page. Because RadListView uses templates, programmatic initialization of templates is supported only when the entire RadListView is created in the code-behind. Please refer to the RadListView programmatic creation help topic.

As for adding your user controls to the listview, you need to instantiate and add an ITemplate object to the RadListView.ItemTemplate property. Then you can initialize your user controls in the ITemplate.InstantiateIn method and add them to the container.

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Amy
Top achievements
Rank 1
answered on 23 Jun 2011, 02:50 PM
Veli,

Thank you for your response.  I had looked at this example previously, but was a bit confused.  I don't have any column titles nor do I have a sql datasource. 

I'm generating a list of ID #'s then for each ID# I need to add two user controls to the listview, each time rebinding the usercontrols so the user controls display the correct information for each ID#.

Do I need to build a datatable first containing the two user controls for each ID# so that I can bind the listview to the datatable?

Also, I'm assuming since I don't have column titles I only need to add one itemtemplaterow and one itemtemplaterowcell?

Is the databinding sub-routine in the ItemTemplate class simply adding the label of the column headers (such as 'Customer ID') or is it also binding the values of the Customer IDs from the database?

As for initializing the user controls in the ITemplate.InstantiateIn method, the only way I know to add user controls in the code-behind using the 'Page.LoadControl' method...I don't think I can use that method in the ItemTemplate class.

Please help!  I'm sorry but I'm still confused as to how to achieve what I'm trying to do.
Thanks,
0
Veli
Telerik team
answered on 24 Jun 2011, 06:55 AM
Hi Amy,

To programmatically add a user control in the InstantiateIn method of the ITemplate class, you can simply cache a reference to the Page in your template class.

The Databinding event handler is used to actually set the data value for the data field (e.g. the value for the 'CustomerID' field).

To better explain this approach, I have created a sample page demonstrating programmatic RadListView initialization where dynamic user controls are loaded in templates. Let me know how this works for you.

Kind regards,
Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Amy
Top achievements
Rank 1
answered on 27 Jun 2011, 07:50 PM
Thank you very much!  I will take a look at the example you provided and let you know how it goes.  Thanks again!
0
Hewitt
Top achievements
Rank 2
answered on 11 Apr 2013, 11:09 AM
I'd love to make use of the Kenetic Scrolling of the RadListView, but populate user controls as easily as using a FlowLayoutPanel, for example.

MyRadlistView.Controls.Add(new MyResourceUserControl)

Would this be something you would consider putting on your road map?
0
Maria Ilieva
Telerik team
answered on 16 Apr 2013, 08:42 AM
Hi Matthew,

We do not have the presented functionality added in our road map for now however I would suggest you to log it in our feedback portal below:
http://feedback.telerik.com/project/108
Thus other users will be able to vote for the feedback ion increase its priority.

Greetings,
Maria Ilieva
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
Amy
Top achievements
Rank 1
Answers by
Veli
Telerik team
Amy
Top achievements
Rank 1
Hewitt
Top achievements
Rank 2
Maria Ilieva
Telerik team
Share this question
or