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

RadPanelItem: Adding controls to ItemTemplate programatically

5 Answers 594 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 19 Aug 2008, 10:13 AM

I have the scenario below:

 ...... inside a RadPanelBar
                <telerik:RadPanelItem Value="SubLocations" Text="Sub Locations" Expanded="true" CssClass="subPanel">  
                    <ItemTemplate> 
                    </ItemTemplate> 
                </telerik:RadPanelItem> 

I want to fill the content of ItemTemplate with a User Control which is decided at runtime. e.g. programatically.

I have tried creating a class derived from ITemplate. e.g. MyTemplateClass. I have overriden the InstantiateIn sub and added my control

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn  
 
        Dim locationsTree As SystemExplorerLocationsTree = _  
                DirectCast(m_Page.LoadControl("~/IpscObject/GUI/SystemExplorer/SystemExplorerLocationsTree.ascx"), SystemExplorerLocationsTree)  
 
        container.Controls.Add(locationsTree)  
 
    End Sub 
 


On the aspx's Page_Init handler. I set RadPanelBar.ItemTemplate to a new instance of MyTemplateClass

locationsPanel.ItemTemplate = new MyTemplateClass() 


However, the InstantiateIn function is not called. What am I doing wrong?

Thanks,
Martin


5 Answers, 1 is accepted

Sort by
0
Paul
Telerik team
answered on 19 Aug 2008, 11:20 AM
Hi Martin,

Please find below a sample code snippet that shows the needed approach.

ASPX:
<form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
    </div> 
    <telerik:RadPanelBar ID="RadPanelBar1" runat="server">  
        <CollapseAnimation Duration="100" Type="None" /> 
        <Items> 
            <telerik:RadPanelItem runat="server" Expanded="True" Text="Root RadPanelItem1">  
                <Items> 
                    <telerik:RadPanelItem runat="server" Value="templateHolder">  
                    </telerik:RadPanelItem> 
                </Items> 
            </telerik:RadPanelItem> 
        </Items> 
        <ExpandAnimation Duration="100" Type="None" /> 
    </telerik:RadPanelBar> 
    <asp:Button ID="Button1" runat="server" Text="Button" /> 
</form> 

Code-behind:
protected void Page_Load(object sender, EventArgs e)  
{  
    RadPanelItem myItem = (RadPanelItem)RadPanelBar1.FindItemByValue("templateHolder");  
    myItem.Controls.Add(LoadControl("~/WebUserControl.ascx"));  


Regards,
Paul
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nick
Top achievements
Rank 2
answered on 09 Feb 2009, 10:43 PM
I'm also adding the raditems dynamically.   I then add a user control to each item.  

The user controls do not stay created - they are lost after every postback.  Is this normal?
0
Paul
Telerik team
answered on 10 Feb 2009, 11:40 AM
Hi Nick,

Please find attached a sample web application that shows the needed approach. Give it a try and let us know what's different in your case.

All the best,
Paul
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Alexander
Top achievements
Rank 1
answered on 13 Jun 2011, 09:16 AM
Hai,

I want to add collapsible panel in runtime with Listbox inside.  For this I am using following code.  This is not getting Expanded/Collapsed on clicking the title bar.    Is there anything wrong in my code.

 

Dim rdPanelBar As New Telerik.Web.UI.RadPanelBar
rdPanelBar.AllowCollapseAllItems = True
rdPanelBar.ExpandMode = Telerik.Web.UI.PanelBarExpandMode.MultipleExpandedItems
rdPanelBar.CollapseDelay = 300
rdPanelBar.ExpandDelay = 300
Dim rdRoot As New Telerik.Web.UI.RadPanelItem
rdRoot.Text = "Investors"
rdRoot.Expanded = True
rdRoot.PreventCollapse = False
rdRoot.Selected = True
rdPanelBar.Items.Add(rdRoot)
Dim rdChild As New Telerik.Web.UI.RadPanelItem
rdChild.Text = ""
rdChild.Expanded = False
rdChild.PreventCollapse = False
rdChild.Selected = True
rdRoot.Items.Add(rdChild)
Dim rdListBox As New Telerik.Web.UI.RadListBox
rdListBox.Height = Unit.Pixel(200)
rdListBox.Width = Unit.Pixel(400)
rdListBox.Items.Add(New Telerik.Web.UI.RadListBoxItem("Fund 1", "1"))
rdListBox.Items.Add(New Telerik.Web.UI.RadListBoxItem("Fund 2", "2"))
rdChild.Controls.Add(rdListBox)
Me.Page.Controls.Add(rdPanelBar)

 

 

When I am using followng code from design time it's working fine.

<telerik:RadPanelBar ID="AA" runat="server" Skin="Forest" AllowCollapseAllItems="true" ExpandMode="MultipleExpandedItems" 
            CollapseDelay="300" ExpandDelay="300">
    <Items>
        <telerik:RadPanelItem runat="server" Text="Funds" Expanded="False" PreventCollapse="false" Selected="True">
            <Items>
                <telerik:RadPanelItem runat="server" Text="" Expanded="true" PreventCollapse="false" Selected="True">
                    <ItemTemplate>
                        <telerik:RadListBox ID="lst" runat="server">
                            <Items >
                                <telerik:RadListBoxItem Text="Item 1" />
                                <telerik:RadListBoxItem Text="Item 2" />
                                <telerik:RadListBoxItem Text="Item 3" />
                                <telerik:RadListBoxItem Text="Item 4" />
                            </Items>
                        </telerik:RadListBox>
                    </ItemTemplate>
                </telerik:RadPanelItem>
            </Items>
        </telerik:RadPanelItem>
    </Items>
</telerik:RadPanelBar>

Please help me to expand/collapse dynamically created PanelBar.  I cann't go with design time Markup because I don't know how many I need to show in runtime.  This comes from database.  This is very high priority for me and please get back to me ASAP.
Thank you.
0
Kate
Telerik team
answered on 16 Jun 2011, 11:26 AM
Hello Alexander,

Please take a look at the following help article where it is fully explained how you can add ItemTemplate to the RadPanelBar using server-side programming.

All the best,
Kate
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.

Tags
PanelBar
Asked by
Martin
Top achievements
Rank 1
Answers by
Paul
Telerik team
Nick
Top achievements
Rank 2
Alexander
Top achievements
Rank 1
Kate
Telerik team
Share this question
or