3 November, 2009
4 Comments
0 categories
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";
Tags: Telerik
This did not work for me. The CommandItemTemplate does not exist in the control.Controls when debugging….
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
Thanks Manish .. working…
Perfect! I use this function in events SqlDataSource_Inserting, Updateting and finding all my template controls.
Thanks
— Alex