Hello,
In most cases, the value is used to access a ToolBar item. If the Value of the RadToolBarDropDown is set by the selected button's value, it will not be a reliable way to identify a dropdown item.
That is why, for obtaining a RadToolBarDropDown item, you can use the Attributes collection of the server-side item object. Here is a simple example of how to do that. Note that you can add them either in the markup or from the code-behind. The runat=server for the item markup is not necessary, it will still work but Visual Studio might underline it as a warning.
<telerik:RadToolBar runat="server" ID="RadToolbar1">
<Items>
<telerik:RadToolBarDropDown data-id="my-dropdown-item">
<Buttons>
<telerik:RadToolBarButton Text="Button markup"></telerik:RadToolBarButton>
</Buttons>
</telerik:RadToolBarDropDown>
</Items>
</telerik:RadToolBar>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var ddl = RadToolbar1.FindItem(x => x.Attributes["data-id"] == "my-dropdown-item") as RadToolBarDropDown;
ddl.Buttons.Add(new RadToolBarButton("Button code behind"));
}
}
FindItemByAttribute can be easily implemented as an extension method until it is built-in:
public static class TelerikToolBarExtensions{
public static RadToolBarItem FindItemByAttribute(this RadToolBar toolbar, string attribute, string value)
{
return toolbar.FindItem(x => x.Attributes["data-id"] == "my-dropdown-item");
}
}
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//var ddl = RadToolbar1.FindItem(x => x.Attributes["data-id"] == "my-dropdown-item") as RadToolBarDropDown;
var ddl = RadToolbar1.FindItemByAttribute("data-id","my-dropdown-item") as RadToolBarDropDown;
ddl.Buttons.Add(new RadToolBarButton("Button code behind"));
}
}
}
Regards,
Peter Milchev
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.