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

Unable to collapse PanelBar

2 Answers 161 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Jo
Top achievements
Rank 1
Jo asked on 11 Feb 2011, 09:43 AM
I have been trying for a couple of days now to get my PanelBar working with databound items, where the item template consists of a user control.  Here is my code:

<telerik:RadPanelBar runat="server" ID="pbSupplierAgreements" AllowCollapseAllItems="true"                 OnItemDataBound="pbSupplierAgreements_ItemDataBound" ExpandMode="SingleExpandedItem"                        Width="100%" DataValueField="ID" DataTextField="SupplierCode" DataTextFormatString="Supplier: {0}">
  <ItemTemplate>
    <telerik:RadPanelItem>
      <Items>
        <telerik:RadPanelItem>
          <ItemTemplate>
            <e:SupplierAgreementView ID="SupplierView" runat="server" />
          </ItemTemplate>
        </telerik:RadPanelItem>
      </Items>
    </telerik:RadPanelItem>
  </ItemTemplate>
</telerik:RadPanelBar>

When the OnItemDataBound is called I bind the data item to the SupplierAgreementView control.

The problem is that this renders the control and data ok , but the panel bar items are all expanded but I can't collapse them and there is no collapse button on the panel bar item headers.

Please help!
Chris
Top achievements
Rank 1
commented on 14 Feb 2011, 04:02 PM

Hi

I'm having pretty much the same issue. Here is my code:
<e:EnumDataSource ID="EnumSource" runat="server" EnumerationTypeName="ExternalContractSupportAPI.Entities.EcsStatus" />
 
    <t:RadPanelBar ID="TermsPanelBar" runat="server" DataTextField="Name" DataSourceID="EnumSource" ExpandMode="SingleExpandedItem">
        <Items>
            <t:RadPanelItem Expanded="true">
                <ContentTemplate>
                    OK: <%#Eval("Name")%>
                </ContentTemplate>
            </t:RadPanelItem>
        </Items>
    </t:RadPanelBar>

The result looks like the attached image: the headers populate correctly but no content is generated.

Any help would be appreciated!

Chris
Yana
Telerik team
commented on 16 Feb 2011, 12:54 PM

Hi Chris,

You cannot set content template globally when the panelbar is databound - in this case the template should be set at runtime. I've attached a simple page to demonstrate the approach, please download it and give it a try.

Regards,
Yana
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.

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 11 Feb 2011, 11:21 AM
Hello Jo,

The reason for this is that you're using templates on the root items.

You should either use item template on the child items or content template on the root items as demonstrated in the example here.

Regards,
Yana
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.
Jo
Top achievements
Rank 1
commented on 11 Feb 2011, 12:26 PM

Hello Yana,

Thanks for replying so promptly!  I have tried changing my items as you suggested but I must be doing something wrong.  I have removed by user control and just put a label in the content template to simplify things.  Here is my code now:

<telerik:RadPanelBar runat="server" ID="pbSupplierAgreements" AllowCollapseAllItems="true" OnItemDataBound="pbSupplierAgreements_ItemDataBound" ExpandMode="SingleExpandedItem" Width="100%" DataValueField="ID">
  <Items>
    <telerik:RadPanelItem>
      <HeaderTemplate>
        Supplier
      </HeaderTemplate>
      <ContentTemplate>
        <telerik:RadPanelItem>
          <ItemTemplate>
            <asp:Label ID="lblSupplierContact" runat="server" Text='<%# Eval("SupplierContact") %>'></asp:Label>
          </ItemTemplate>
        </telerik:RadPanelItem>
      </ContentTemplate>
    </telerik:RadPanelItem>
  </Items>
</telerik:RadPanelBar>


In my code behind I am setting the data source of the panel to be an IList<myComplexType> and calling DataBind().  But when the page loads I just see two collapsed items, with the name of the type as the header.  The label is not rendered either and I can't expand the items.
Yana
Telerik team
commented on 16 Feb 2011, 09:49 AM

Hello Jo,

You shouldn't place RadPanelItem in the content template - you can directly place the controls:

<telerik:RadPanelBar runat="server" ID="pbSupplierAgreements" AllowCollapseAllItems="true"
    OnItemDataBound="pbSupplierAgreements_ItemDataBound" ExpandMode="SingleExpandedItem"
    Width="100%" DataValueField="ID">
    <Items>
        <telerik:RadPanelItem>
            <HeaderTemplate>
                Supplier
            </HeaderTemplate>
            <ContentTemplate>
                <asp:Label ID="lblSupplierContact" runat="server" Text='<%# Eval("SupplierContact") %>'></asp:Label>                     
            </ContentTemplate>
        </telerik:RadPanelItem>
    </Items>
</telerik:RadPanelBar>


Best wishes,
Yana
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.
Manager
Top achievements
Rank 1
commented on 24 Dec 2022, 08:07 PM

Hi ,

I also had the same problem like Jo and Chris - the content of the panel bar not generated,

I tried the solution that tells to set the content template at run time , but still the content isn't generated ,

(if using itemtemplate instead of contenttemplate - the content is generated but doesnwt get collapsed which is also bad -  both in client side or in server side)

I would to get a propper solution ,

Thanks.

0
Attila Antal
Telerik team
answered on 29 Dec 2022, 04:31 PM

Hi,

Generally speaking, enabling Templates for a Control will result in avoiding the built-in styles, functionalities, etc. Controls added to the Templates have no effect on the PanelBar's functionality. If the PanelBar is not working, that is likely due to the incorrect configuration of the PanelBar and its Templates.

This is an example of a code showing how the Panelbar behaves by design.

<telerik:RadPanelBar ID="RadPanelBar1" runat="server">
    <Items>
        <telerik:RadPanelItem Text="Item without Content" />
        <telerik:RadPanelItem Text="Item wint a Content">
            <ContentTemplate>
                Item Content
            </ContentTemplate>
        </telerik:RadPanelItem>
        <telerik:RadPanelItem Text="Item with Child Items">
            <Items>
                <telerik:RadPanelItem Text="Child Item with Content">
                    <ContentTemplate>
                        Child Item Content
                    </ContentTemplate>
                </telerik:RadPanelItem>
                <telerik:RadPanelItem Text="Item 2.2"></telerik:RadPanelItem>
            </Items>
        </telerik:RadPanelItem>
        <telerik:RadPanelItem Text="Item with a Content and Custom Header Template">
            <HeaderTemplate>
                Item Header
            </HeaderTemplate>
            <ContentTemplate>
                Item Content
            </ContentTemplate>
        </telerik:RadPanelItem>
        <telerik:RadPanelItem Text="Item with ItemTemplate and HeaderTemplate">
            <HeaderTemplate>
                Item with Header & Item Templates
            </HeaderTemplate>
            <ItemTemplate>
                Item Template
            </ItemTemplate>
        </telerik:RadPanelItem>
    </Items>
</telerik:RadPanelBar>

 

A couple of key points on the Templates:

  • To display content in a collapsible panel, use ContentTemplates.
  • If creating templates dynamically, create everything dynamically, otherwise create everything in the markup.
  • Use ItemTemplates only if you'd like to create a custom item replacing the built-in PanelBarItem. Its content and expand/collapse functionality will also need to be implemented.

 

Here is an example of adding custom Content dynamically the proper way:

<asp:Panel ID="Panel1" runat="server"></asp:Panel>

 

C#

protected void Page_Init(object sender, EventArgs e)
{
    var rpb = new RadPanelBar() { ID = "RadPanelBar1", DataFieldID = "ID", DataFieldParentID = "ParentID", DataTextField = "Text", DataValueField = "Value", Width = Unit.Percentage(100) };

    rpb.DataSource = GetData();
    rpb.DataBind();

    foreach (RadPanelItem panelItem in rpb.Items)
    {
        BuildItemsRecursive(panelItem);
    }

    Panel1.Controls.Add(rpb);
}
private void BuildItemsRecursive(RadPanelItem panelItem)
{
    if (panelItem.Items.Count > 0)
    {
        foreach (RadPanelItem childItem in panelItem.Items)
        {
            BuildItemsRecursive(childItem);
        }
    }
    else
    {
        MyContentTemplate contentTemplate = new MyContentTemplate(string.Format("<p>Custom Content for Item <strong>{0}</strong></p>", panelItem.Text));

        panelItem.ContentTemplate = contentTemplate; // set the ContentTemplate
        panelItem.ContentTemplate.InstantiateIn(panelItem); // call the Instantiate function

        panelItem.DataBind(); // bind the item
    }
}
class MyContentTemplate : ITemplate
{
    string _content;
    public MyContentTemplate(string content)
    {
        _content = content;
    }
    public void InstantiateIn(Control container)
    {
        container.Controls.Add(new Literal() { Text = _content });
    }
}
private List<MyObject> GetData()
{
    List<MyObject> objectList = new List<MyObject>();

    objectList.Add(new MyObject() { ID = 1, ParentID = null, Text = "Root Item 1", Value = "R1" });
    objectList.Add(new MyObject() { ID = 2, ParentID = 1, Text = "Child 1 of Root Item 1", Value = "C1R1" });
    objectList.Add(new MyObject() { ID = 3, ParentID = 1, Text = "Child 2 of Root Item 1", Value = "C2R1" });
    objectList.Add(new MyObject() { ID = 4, ParentID = null, Text = "Root Item 2", Value = "R2" });
    objectList.Add(new MyObject() { ID = 5, ParentID = null, Text = "Root Item 3", Value = "R3" });
    objectList.Add(new MyObject() { ID = 6, ParentID = 5, Text = "Child 1 of Root Item 3", Value = "C1R3" });
    objectList.Add(new MyObject() { ID = 7, ParentID = 5, Text = "Child 2 of Root Item 3", Value = "C2R3" });
    objectList.Add(new MyObject() { ID = 8, ParentID = 6, Text = "Child 1 of Child Item 1 of Root Item 3", Value = "C1C1R3" });
    objectList.Add(new MyObject() { ID = 9, ParentID = 6, Text = "Child 2 of Child Item 1 of Root Item 3", Value = "C2C1R3" });

    return objectList;
}
class MyObject
{
    public int ID { get; set; }
    public int? ParentID { get; set; }
    public string Text { get; set; }
    public string Value { get; set; }
}

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PanelBar
Asked by
Jo
Top achievements
Rank 1
Answers by
Yana
Telerik team
Attila Antal
Telerik team
Share this question
or