How to find Control in TemplateField of GridView?
Try this:
foreach(GridViewRow row in GridView1.Rows) { if(row.RowType == DataControlRowType.DataRow) { HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink; }}
If you are handling RowDataBound event, it's like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ if(e.Row.RowType == DataControlRowType.DataRow) { HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink; }}
You can use this code to find HyperLink
in GridView. Use of e.Row.Cells[0].Controls[0]
to find First position of control in GridView.
protected void AspGrid_RowDataBound(object sender, GridViewRowEventArgs e){ if(e.Row.RowType == DataControlRowType.DataRow) { DataRowView v = (DataRowView)e.Row.DataItem; if (e.Row.Cells.Count > 0 && e.Row.Cells[0] != null && e.Row.Cells[0].Controls.Count > 0) { HyperLink link = e.Row.Cells[0].Controls[0] as HyperLink; if (link != null) { link.Text = "Edit"; } } }}
I have done it accessing the controls inside the cell control. Find in all control collections.
ControlCollection cc = (ControlCollection)e.Row.Controls[1].Controls; Label lbCod = (Label)cc[1];