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

Adding button control to dynamically created RadPanelBar

4 Answers 290 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 19 Sep 2012, 03:07 PM
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.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Sep 2012, 05:02 AM
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.
0
Eric
Top achievements
Rank 1
answered on 25 Sep 2012, 07:00 PM
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


0
Eric
Top achievements
Rank 1
answered on 02 Oct 2012, 01:27 PM
Can anyone help me with this???
0
imran
Top achievements
Rank 1
answered on 08 Aug 2013, 09:51 AM
Try to write this code in Page_Init instead of Page_Load
Tags
PanelBar
Asked by
Eric
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Eric
Top achievements
Rank 1
imran
Top achievements
Rank 1
Share this question
or