Adding button control to dynamically created RadPanelBar

5 posts, 0 answers
  1. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 19 Sep 2012 Link to this post

    Markup .aspx
    <telerik:RadPanelBar ID="PanelBar1" Height="100px" Width="100%" EnableEmbeddedSkins="false" Skin="MyCustonSkin" runat="server">
    </telerik:RadPanelBar>


    Code Behind .aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        RadPanelItem myProfilePanelItem = new RadPanelItem();
        myProfilePanelItem.Text = "My Profile";
     
        RadPanelItem myFavoritesPanelItem = new RadPanelItem();
        myFavoritesPanelItem.Text = "My Favorites";
        Button btnSearchFavorites = new Button();
        btnSearchFavorites.Text = "Search more favorites";
        btnSearchFavorites.CssClass = "ButtonStyle";
        btnSearchFavorites.Click += new EventHandler(btnSearchFavorites_Click);
        myFavoritesPanelItem.Controls.Add(btnSearchFavorites);
     
        PanelBar1.Items.Add(myProfilePanelItem);
        PanelBar1.Items.Add(myFavoritesPanelItem);
    }


    protected void btnSearchFavorites_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Favorites", false);
    }

    The problem is that the btnSearchFavorites_Click event is not firing when the button is clicked.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Sep 2012 Link to this post

    Hi Eric,

    Templates could be added to RadPanelBar at runtime, using the ItemTemplate property. Try adding the button in a as shown below.
    C#:
    CustomContentTemplate template = new CustomContentTemplate();
    protected void Page_Load(object sender, EventArgs e)
    {
      foreach (RadPanelItem item in PanelBar1.Items)
      {
        item.ContentTemplate = new CustomContentTemplate();
        template.InstantiateIn(item);
        item.DataBind();
      
    }
    class CustomContentTemplate : ITemplate
    {
       public void InstantiateIn(Control container)
       {
                Button btnSearchFavorites = new Button();
                btnSearchFavorites.Text = "Search more favorites";
                btnSearchFavorites.CssClass = "ButtonStyle";
                btnSearchFavorites.Click += new EventHandler(btnSearchFavorites_Click);
                container.Controls.Add(btnSearchFavorites);
       }
       void btnSearchFavorites_Click(object sender, EventArgs e)
       {
       }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 25 Sep 2012 Link to this post

    rhfg
    I get the following error if I do this:

    "Cannot set ContentTemplate on a RadPanelItem, which has child Items."


    I forgot to mention that myFavoritesPanelItem  has child items, and all items are created dynamically in code behind because most items are dynamic (come from the database)

    Here's my structure:

    Item 1

    Item 2

    Item 3
    Item 1
    Item 2
    Item 3 -> SEARCH MORE FAVORITES BUTTON

    Item 4
    Item 1
    Item 2
    Item 3


  5. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 02 Oct 2012 Link to this post

    Can anyone help me with this???
  6. imran
    imran avatar
    1 posts
    Member since:
    Jan 2007

    Posted 08 Aug 2013 Link to this post

    Try to write this code in Page_Init instead of Page_Load
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017