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

RadToolBarButton Dynamic ItemTemplate

3 Answers 234 Views
ToolBar
This is a migrated thread and some comments may be shown as answers.
Wes
Top achievements
Rank 1
Wes asked on 27 Aug 2012, 08:47 PM
I have been trying to create a RadToolBarButton Dynamically in C# with an ItemTemplate.  Does anyone have an example of how to do the following in code behind?

  <telerik:RadToolBar runat="server" ID="toolBarKeywordSearch" EnableRoundedCorners="true" Style="height35px;"
                EnableShadows="true" OnButtonClick="toolBarWorkItem_Onclick">
                <Items>
                    <telerik:RadToolBarButton Value="gridToolBarButton" CommandName="SearchTextEntered">
                        <ItemTemplate>
                            <asp:Label ID="lblKeyWord" runat="server" Text="Keyword search:"></asp:Label>
                            <telerik:RadTextBox ID="txtSearchQuery" runat="server" Width="110px" ToolTip="Type to search" />
                        </ItemTemplate>
                    </telerik:RadToolBarButton>
                     </Items>
            </telerik:RadToolBar>

Thanks,

Wes

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 28 Aug 2012, 05:30 AM
Hi Wes,

Try the following code snippet to achieve your scenario.

C#:
RadToolBarButton ToolBarButton = new RadToolBarButton();
RadToolBar toolBarKeywordSearch = new RadToolBar();
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            toolBarKeywordSearch.ID = "toolBarKeywordSearch";
            toolBarKeywordSearch.EnableRoundedCorners = true;
            toolBarKeywordSearch.Attributes.Add("Style", "height: 35px;");
            toolBarKeywordSearch.EnableShadows = true;
            toolBarKeywordSearch.ButtonClick += new RadToolBarEventHandler(toolBarKeywordSearch_ButtonClick);
            ToolBarButton.Value = "gridToolBarButton";
            ToolBarButton.CommandName = "SearchTextEntered";
            toolBarKeywordSearch.Items.Add(ToolBarButton);
            form1.Controls.Add(toolBarKeywordSearch);
        }
        TextBoxTemplate template = new TextBoxTemplate();
        template.InstantiateIn(ToolBarButton);
        toolBarKeywordSearch.DataBind();
    }
void toolBarKeywordSearch_ButtonClick(object sender, RadToolBarEventArgs e)
    {
      // your code here
    }
 
 
class TextBoxTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        Label lblKeyWord = new Label();
        lblKeyWord.ID = "lblKeyWord";
        lblKeyWord.Text = "Keyword search:";
        container.Controls.Add(lblKeyWord);
        RadTextBox txtSearchQuery = new RadTextBox();
        txtSearchQuery.ID = "txtSearchQuery";
        txtSearchQuery.Width = Unit.Pixel(110);
        txtSearchQuery.ToolTip = "Type to search";
        container.Controls.Add(txtSearchQuery);
    }
}

Please take a look into this documentation for more information.

Hope this helps.

Regards,
Princy.
0
Wes
Top achievements
Rank 1
answered on 28 Aug 2012, 03:31 PM
I can't believe I missed that link.  I should get more sleep.

Thanks for your help it works great!
0
Paul
Top achievements
Rank 1
answered on 28 Jul 2016, 08:45 AM
Thank you this also helped me. I must have missed that Doc too.
Tags
ToolBar
Asked by
Wes
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Wes
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Share this question
or