4 Comments

If you want to change the controls in the CommandItemTemplate of a Telerik RadGrid, you will have to use a recursive FindControl function, because ASP .NET 2.0 does not ship with a FindControl function that can search for controls in child controls.

     public static class ControlFinder
    {
        /// <summary>
        /// Recursively finds a control in the controls collection.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Control FindControlRecursive(Control control, string id)
        {
            // Return null if parameter control is null
            if (control == null) return null;

            // Try to find the control at the current level
            Control ctrl = control.FindControl(id);
            if (ctrl == null)
            {
                // Loop through child controls
                foreach (Control child in control.Controls)
                {
                    // Try to find the control at the next level
                    ctrl = FindControlRecursive(child, id);

                    // Stop search when control is found
                    if (ctrl != null) break;
                }
            }
            return ctrl;        }
    }

In the page load:

LinkButton showReports = ControlFinder.FindControlRecursive(this.RadGrid.MasterTableView, "showReports") as LinkButton;
showReports.Attributes["href"] = @"\\MyShare";

4 Replies to “Change Telerik RadGrid CommandItemTemplate controls on page load with a recursive FindControl function”

  1. This did not work for me. The CommandItemTemplate does not exist in the control.Controls when debugging….

  2. Hi ,
    You can instead use this event & can find control in Command Item Template.
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
    if (e.Item is GridCommandItem)
    {
    GridCommandItem commandItem = e.Item as GridCommandItem;
    LinkButton button = commandItem.FindControl(“LinkButton1”)as LinkButton;

    button.CommandName = “MyCommandName”;
    button.Text = “Perform custom operation”;
    }
    }

    Thanks
    –Manish Sahare

  3. Perfect! I use this function in events SqlDataSource_Inserting, Updateting and finding all my template controls.

    Thanks
    — Alex

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts