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];

protected void gvTurnos_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {

        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            LinkButton btn = (LinkButton)e.Row.FindControl("btnAgregarVacio");
            if (btn != null)
            {
                btn.Visible = rbFiltroEstatusCampus.SelectedValue == "1" ? true : false;
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Try with below code.

Like GridView in LinkButton, Label, HtmlAnchor and HtmlInputControl.

<asp:GridView ID="mainGrid" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered table-hover tablesorter"
    OnRowDataBound="mainGrid_RowDataBound"  EmptyDataText="No Data Found.">
    <Columns>
        <asp:TemplateField HeaderText="HeaderName" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblName" Text=' <%# Eval("LabelName") %>'></asp:Label>
                <asp:LinkButton ID="btnLink" runat="server">ButtonName</asp:LinkButton>
                <a href="javascript:void(0);" id="btnAnchor" runat="server">ButtonName</a>
                <input type="hidden" runat="server" id="hdnBtnInput" value='<%#Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Handling RowDataBound event,

protected void mainGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblName = (Label)e.Row.FindControl("lblName");
        LinkButton btnLink = (LinkButton)e.Row.FindControl("btnLink");
        HtmlAnchor btnAnchor = (HtmlAnchor)e.Row.FindControl("btnAnchor");
        HtmlInputControl hdnBtnInput = (HtmlInputControl)e.Row.FindControl("hdnBtnInput");
    }
}

Comments

  1. Mancini

    • 2021/7/10

    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.

  2. Zion

    • 2020/10/6

    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];

  3. Leonel

    • 2020/8/9

    List<string> AssIds = new List<string>(); for (int i = 0; i < grdApproval.Rows.Count; i++) { LinkButton lblAssId = (LinkButton)grdApproval.Rows[ 

  4. Musa

    • 2015/8/23

    The hyperlink hlPlus is a hyperlink field inside GridView‘s Template Field(Item Template). Solution. 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:

  5. Kelly

    • 2020/1/25

    Re: asp.net gridview findcontrol in itemtemplate When do you want to access it? Typically, the FindControl method is used when accessing 

  6. Stewart

    • 2020/1/23

    You should use FindControl from specific GridViewRow. GridView has many rows. You need to bind the GridView first, then find the ddl in every row by iterating rows. Or use RowDataBound event of the GridView and use e.Row.FindControl, make sure you only find the ddl if RowType is DataRow, because some row is header or footer.

  7. Roger

    • 2019/3/1

    The following HTML Markup consists of an ASP.Net GridView with two TemplateField columns, one consisting of a TextBox and other a DropDownList control. The 

  8. Derrick

    • 2017/6/18

    1. Try this to find a control in the HeaderTemplate without a row-data-bind, if that's what is needed: private void Lab_1_GV1_Populate_SearchText () { GridView GV1 = (GridView)FindControl ("Lab_1_GV1"); TextBox TXB1 = (TextBox)GV1.HeaderRow.FindControl ("Lab_1_TX2GV1"); } Thanks.

  9. George

    • 2016/12/31

    How to find control in GridView EditItemTemplate in ASP.Net < asp:TemplateField HeaderText = "Invoice_No" ItemStyle-Width = "150" >. < ItemTemplate >.

  10. Carson

    • 2020/3/31

    Solution 1. foreach (GridViewRow gdv in GridView1.Rows) { if (gdv.RowType == DataControlRowType.Header) { //Find the control here. TextBox txt = (TextBox)gdv.HeaderRow.FindControl ( "txt" ); } } Please Sign up or sign in to vote.

  11. Myles

    • 2018/2/21

    TemplateField // column fields in the GridView control. TextBox lastName = (TextBox)row.FindControl("LastNameTextBox"); TextBox firstName = (TextBox)row 

  12. Stephen

    • 2020/11/17

    For retrieving values do this: for (int i = 0; i < GridView2.Rows.Count; i++) { //extract the TextBox values Label lblname= (Label)GridView2.Rows [i].Cells [0].FindControl ("lblname"); Label lblPickUpPoint= (Label)GridView2.Rows [i].Cells [0].FindControl ("lblPickUpPoint"); //Do your excel binding here }

  13. David

    • 2020/6/8

    How to find Control in TemplateField of GridView? Solution: Try this: foreach(GridViewRow row in GridView1.Rows) { if(row.RowType == DataControlRowType.DataRow) 

  14. Jaiden

    • 2018/6/4

    Find (Access) control inside GridView in RowCommand event of ASP.Net GridView. The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is determined. The TextBox and DropDownList controls are referenced using the FindControl method of the GridView Row by passing the ID of the control as parameter.

  15. Joshua

    • 2020/5/16

    This tutorial we have learn How to find Control in TemplateField of GridView.Here we have explain attendance system using the .net control 

  16. Azrael

    • 2015/2/1

    This article will show you how you can find the itemtemplate control value on gridview RowDataBound in asp.net using C#.net and VB.net.

  17. Roger

    • 2020/8/3

    We will do this by using Findcontrol Method present inside the GridView. You can use this method for any other control inside which you have 

Comments are closed.

Recent Posts