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

Manipulate HeaderTemplate in code behind

1 Answer 302 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Vincent
Top achievements
Rank 1
Vincent asked on 18 May 2011, 05:15 PM
Hello,

I have an empty PanelBar in my aspx page and I want to fill it in the code behind.
I successfully fill the PanelBar with PanelItems but I don't understand how to build th HeaderTemplate and Contentemplate.
This work :
RadPanelItem myPanelItem = new RadPanelItem("First item")
myPanelBar.Items.Add(myPanelItem);

But when I try to add the headerTemplate like that :
RadPanelItem myPanelItem = new RadPanelItem("First item")
 
Button btn = new Button();
btn.Text = "Test";
myPanelItem.Header.Controls.Add(btn);
 
myPanelBar.Items.Add(myPanelItem);


Visual studio say that the Header is null and the header can't be instanciate.

So I try others way but nothing successfull
Is someone has any solution ?

1 Answer, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 25 May 2011, 02:35 PM
Hello Vincent,

In order to add a HeaderTemplate to RadPanelItem dynamically you need to follow these steps:
1. At first create a new class that implements the System.Web.UI.ITemplate interface:
public class RadPanelItemHeaderTemplate : ITemplate

2. In the class, implement the InstantiateIn method, which is a member of the ITemplate interface. This method provides a way to insert an instance of text and controls into the specified container.
3. In the InstantiateIn method, create the controls for the template item, set their properties, and then add them to the parent's Controls collection:
public void InstantiateIn(Control container)
{
    
    Button someButton = new Button();
    someButton.Text = "Test";
    someButton.Click += new EventHandler(someButton_Click);
    container.Controls.Add(someButton);
     
 
    if (_Text1 != String.Empty)
    {
        Label someLabel = new Label();
        someLabel.Text = "  " + _Text1;
        container.Controls.Add(someLabel);
    }
 
}

4. Then at Page.OnInit event handler you can add RadPanelBar items, set the HeaderTemplate property of the item/items and call ApplyHeaderTemplate() method in this way:
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
 
    RadPanelItem item1 = new RadPanelItem("first item");
    this.RadPanelBar1.Items.Add(item1);
 
    item1.HeaderTemplate = new RadPanelItemHeaderTemplate();
    item1.ApplyHeaderTemplate();
 
    item1.Items.Add(new RadPanelItem("child item 1"));
    item1.Items.Add(new RadPanelItem("child item 2"));
 
    RadPanelItem item2 = new RadPanelItem("second item");
    this.RadPanelBar1.Items.Add(item2);
}

I prepared a small sample  – please find it attached.

Regards,
Kalina
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
Vincent
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Share this question
or