RadToolBarButton Dynamic ItemTemplate

4 posts, 1 answers
  1. Wes
    Wes avatar
    5 posts
    Member since:
    May 2009

    Posted 27 Aug 2012 Link to this post

    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
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Aug 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Wes
    Wes avatar
    5 posts
    Member since:
    May 2009

    Posted 28 Aug 2012 Link to this post

    I can't believe I missed that link.  I should get more sleep.

    Thanks for your help it works great!
  5. Paul
    Paul avatar
    25 posts
    Member since:
    May 2016

    Posted 28 Jul Link to this post

    Thank you this also helped me. I must have missed that Doc too.
Back to Top