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
khanahmed
Member
328 Points
135 Posts
asp.net gridview findcontrol in itemtemplate
May 02, 2011 10:47 AM|LINK
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="-----------------.aspx.cs" Inherits="--------------------------" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="grvReqCumProjectReport" runat="server"
AutoGenerateColumns="False" DataKeyNames="RequestId"
onrowcommand="grvReqCumProjectReport_RowCommand"
onrowdatabound="grvReqCumProjectReport_RowDataBound">
<AlternatingRowStyle CssClass="AlternatingRowStyle"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Edit Request" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEditRequest" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="HeaderStyle"></HeaderStyle>
<RowStyle CssClass="RowStyle"></RowStyle>
</asp:GridView>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
</asp:Content>
I wanna access lnkBtnEditRequest highlighted in bold.
vinz
All-Star
127087 Points
17946 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