Manipulate HeaderTemplate in code behind

2 posts, 0 answers
  1. Vincent
    Vincent avatar
    3 posts
    Member since:
    May 2011

    Posted 18 May 2011 Link to this post

    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 ?
  2. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 25 May 2011 Link to this post

    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.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top