New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Adding Templates at Runtime
The following help article demonstrates how you can add templates to RadRibbonBar control at runtime, using the RibbonBarTemplateItem.
The RibbonBarTemplateItem should be initialized in the OnInit event of the page. This is needed as the template should be instantiated before the elements of the RadRibbonBar are initialized. The tabs and groups should be dynamically added so that templates can be defined at run time.
ASPNET
<telerik:RadRibbonBar RenderMode="Lightweight" ID="RadRibbonBar1" runat="server" />
C#
protected override void OnInit(EventArgs e)
{
//RibbonBar properties
RadRibbonBar1.Skin = "Default";
RadRibbonBar1.Width = Unit.Percentage(100);
//creating RibbonBar tab
RibbonBarTab radRibbonBarTab = new RibbonBarTab();
radRibbonBarTab.ID = "HomeID";
radRibbonBarTab.Value = "HomeValue";
radRibbonBarTab.Text = "Home";
RadRibbonBar1.Tabs.Add(radRibbonBarTab);
//creating RibbonBar group
RibbonBarGroup radRibbonBarGroup = new RibbonBarGroup();
radRibbonBarGroup.ID = "GroupID";
radRibbonBarGroup.Value = "GroupValue";
radRibbonBarGroup.Text = "Group";
radRibbonBarTab.Groups.Add(radRibbonBarGroup);
//creating RibbonBar button
RibbonBarButton btnRibbonBarButton = new RibbonBarButton();
btnRibbonBarButton.ID = "btn1";
btnRibbonBarButton.Value = "ribbonBtn1";
btnRibbonBarButton.Enabled = true;
btnRibbonBarButton.Size = RibbonBarItemSize.Large;
btnRibbonBarButton.Text = "Ribbon Button";
radRibbonBarGroup.Items.Add(btnRibbonBarButton);
//creating RibbonBar template
RibbonBarTemplateItem radRibbonBarTemplateItem = new RibbonBarTemplateItem();
radRibbonBarTemplateItem.Template = new RibbonCheckBox("checkbox", "CheckBox Text", false, true);
//radRibbonBarTemplateItem.Template = new RibbonCheckBox("checkbox", "CheckBox Text", false, false, true);
radRibbonBarGroup.Items.Add(radRibbonBarTemplateItem);
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
class RibbonCheckBox : ITemplate
{
private string _strID;
private string _strText;
private bool _bEnabled;
private bool _bChecked;
private bool _bAddOnClickEvent;
// public RibbonCheckBox(string strID, string strText, bool bChecked, bool bAddOnClickEvent, bool bEnabled)
public RibbonCheckBox(string strID, string strText, bool bChecked, bool bEnabled)
{
_strID = strID;
_strText = strText;
_bEnabled = bEnabled;
_bChecked = bChecked;
//_bAddOnClickEvent = bAddOnClickEvent;
}
public void InstantiateIn(Control htmlControlContainer)
{
CheckBox htmlCheckBox = new CheckBox();
htmlCheckBox.ID = _strID;
htmlCheckBox.Text = _strText;
htmlCheckBox.Enabled = _bEnabled;
htmlCheckBox.Checked = _bChecked;
htmlCheckBox.CssClass = "ribbonCheckBox";
//if (_bAddOnClickEvent)
// htmlCheckBox.Attributes.Add("onclick", "RibbonCheckBoxClick('" + _strID + "', this.checked);");
htmlControlContainer.Controls.Add(htmlCheckBox);
}
}