Search Control in GridView in asp.net
-
Hello All, I need to search recursive control inside gridview i have written below code but i can not able to find the control in which are define in I have taken on linkbutton inside templated field but when i try to search control but i didn't get. Code : public static Control FindControlRecursive(Control Root, string Id) { if (Root.ID == Id) return Root; foreach (Control Ctl in Root.Controls) { Control FoundCtl = FindControlRecursive(Ctl, Id); if (FoundCtl != null) return FoundCtl; } return null; } but it return null while i am try to find out control like LinkButton B = FindControlRecursive(GridView1, "lnk") as LinkButton; can you please help how to resolved above issue? Thanks in advance,
Anish Patel
-
Hello All, I need to search recursive control inside gridview i have written below code but i can not able to find the control in which are define in I have taken on linkbutton inside templated field but when i try to search control but i didn't get. Code : public static Control FindControlRecursive(Control Root, string Id) { if (Root.ID == Id) return Root; foreach (Control Ctl in Root.Controls) { Control FoundCtl = FindControlRecursive(Ctl, Id); if (FoundCtl != null) return FoundCtl; } return null; } but it return null while i am try to find out control like LinkButton B = FindControlRecursive(GridView1, "lnk") as LinkButton; can you please help how to resolved above issue? Thanks in advance,
Anish Patel
Hi! in future post, try using the code block :-D just for better understanding of the code :thumbsup: Well, the Control, must be a GridView, so you can add
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return ((GridView)Root);
if (Root is GridView)//or some like that (i don't remember well)
{
GridView gv = ((GridView)Root);
Int32 num = 3;//Based zero, number of column where is the link button column
Int32 row = 1;//Based zero, number of row where is the link button row
Control FoundCtl = ((Control)gv.Rows[row].Cells[num].FindControl(Id));
if (FoundCtl != null)
return FoundCtl;
}
return null;
}Remember that in ASP.NET the dinamically controls added like in grid view Template field, have the same name, except in Client-Side. if you want a especific row, you only need to change the row parameter ;)