Accessing CommandItemTemplate

9 posts, 1 answers
  1. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 05 Aug 2010 Link to this post

    I have defined a custom CommandItemTemplate which contains a LinkButton

    <CommandItemTemplate>
      <div style="padding: 5px 5px;">
        <asp:LinkButton ID="linkBtnAddReport" runat="server" CommandName="InitInsert">
          <img style="border:0px;vertical-align:middle;" alt="" src="../../CSS/images/add.gif" /> Add New Report
        </asp:LinkButton>
      </div>
    </CommandItemTemplate>

    I need to change the Enabled property but I need to change it outside of the GridItemCommand event.

    How can I find the LinkButton outside of the GridItemCommand event?

    Thanks.
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Aug 2010 Link to this post

    Hello Ryan,


    Use the following code snippet to access the LinkButton placed in CommandItemTemplate.

    C#:
    GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
    LinkButton lnkButton = (LinkButton)cmdItem.FindControl("linkBtnAddReport");
    lnkButton.Enabled = false;


    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ryan
    Ryan avatar
    33 posts
    Member since:
    May 2009

    Posted 05 Aug 2010 Link to this post

    Thanks Shinu
  5. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 15 Dec 2010 Link to this post

    Hi, i need something similar. I have CommandItemTemplate of such kind:
    <CommandItemTemplate>
        <XXX:FilterBar runat="server" />
        <XXX:HeaderBar runat="server" />
    </CommandItemTemplate>
    Why I need this - because in FilterBar I might have a set of various controls for filtering grid data: textboxes, combos, etc.

    HeaderBar should be filled programmatically.

    I tried to access it from OnLoad event, but
    MasterTableView.GetItems(GridItemType.CommandItem)
    returns zero elements!

    Maybe there's a better place to find HeaderBar control inside CommandItemTemplate and add controls to it?
  6. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 15 Dec 2010 Link to this post

    Hello Alexander,

    Try accessing it in the grid PreRender event, e.g. after the grid is once bound. On initial page load the grid is still not bound and its items are not created.

    Greetings,
    Iana
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  7. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 15 Dec 2010 Link to this post

    Thanks. I tried, but in PreRender it's zero either. So the problem is somewhere else...
  8. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 15 Dec 2010 Link to this post

    In exprerimental way, discovered at last, how to make it work in ItemCreated event:
    var headerBar_ = item.Controls[0].Controls.OfType<HeaderBar>().First();
    Strange... Why I must access some sub controls, indexes, instead of accessing items directly via some property? What if some other time will be more than 1 controls in command item template somehow? :(
    Well, all-in-all such approach seems very unreliable...
  9. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 20 Dec 2010 Link to this post

    Hi Alexander,

    In the ItemCreated event, you should be able to access the GridCommandItem and its controls as below:

    protected void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
    {
        if(e.Item is GridCommandItem)
        {
            GridCommandItem commandItem = e.Item as GridCommandItem;
            LinkButton lnk = commandItem.FindControl("lnk_ID") as LinkButton;
            .....
        }
    }

    You can refer to the below article for more information:
    http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

    All the best,
    Iana
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  10. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 23 Dec 2010 Link to this post

    Yeap, i've already understood the only possible way to distinct CommandItemTemplate was ItemCreated event... Ok :)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017