Finding a control within a GridView's TemplateField can be achieved using the FindControl() method.
In this example, we have a GridView with a TemplateField that contains a HyperLink. The task is to access and manipulate the HyperLink using code-behind.
The following code demonstrates how to access the HyperLink control in the RowDataBound event of the GridView:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink; } }
Once you have a reference to the HyperLink control, you can set its properties, handle events, or perform other operations as needed. For instance, you can set the NavigateUrl property to specify the URL to navigate to when the link is clicked.
HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink; myHyperLink.NavigateUrl = "http://www.example.com";
It's important to note that the FindControl() method only searches within the scope of the current data row. If the control is located in a nested TemplateField, you may need to use the FindControlRecursive() method.
The above is the detailed content of How to Find and Use Controls within a GridView's TemplateField?. For more information, please follow other related articles on the PHP Chinese website!