When do you want to access it? Typically, the FindControl method is used when accessing controls within Data Representation controls or wthin a naming container hierarchy.
Here's an example for accessing it at RowDataBound event of GridView:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
LinkButton link = (LinkButton)e.Row.FindControl("lnkBtnEditRequest");
//do something with the link here
}
}
You can also access the control outside RowDataBound event after the GridView is binded with data.
//accessing specific control
// just change the index of cells based on your requirements
LinkButton link = (LinkButton)GridView1.Rows[0].Cells[0].FindControl("lnkBtnEditRequest");
//if you want to access multiple rows then you can loop through the gridview rows and then do the findcontrol within your loop
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: asp.net gridview findcontrol in itemtemplate
May 02, 2011 11:30 AM|LINK
When do you want to access it? Typically, the FindControl method is used when accessing controls within Data Representation controls or wthin a naming container hierarchy.
Here's an example for accessing it at RowDataBound event of GridView:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton link = (LinkButton)e.Row.FindControl("lnkBtnEditRequest"); //do something with the link here } }You can also access the control outside RowDataBound event after the GridView is binded with data.
//accessing specific control // just change the index of cells based on your requirements LinkButton link = (LinkButton)GridView1.Rows[0].Cells[0].FindControl("lnkBtnEditRequest"); //if you want to access multiple rows then you can loop through the gridview rows and then do the findcontrol within your loopMessageBox Controls for WebForms | Blog | Twitter | Linkedin