what is "FindControl" in gridview editing?

Posted by Shreesh under ASP.NET on 10/27/2011 | Points: 10 | Views : 2772 | Status : [Member] | Replies : 1
can someone tell me why and what is "FindControl" in gridview editing?


Posted by: Thiru on: 10/27/2011 [Member] Starter | Points: 25

Hi Shreesh,

FindControl in gridview is used to find the controls (like: label, textbox etc) we used in the GridView

For Example:
<asp:GridView ID="GridViewCalEvents" runat="server" AutoGenerateColumns="False" 

AutoGenerateSelectButton="True" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
<RowStyle BackColor="#F7F7DE" />
<asp:TemplateField HeaderText="ID">
<asp:Label ID="id" runat="server" Text='<%# Eval("sno")%>'></asp:Label>

<asp:TemplateField HeaderText="Date">
<ItemTemplate><asp:Label ID="lbldtp" runat="server" Text='<%# Eval("dtp") %>'></asp:Label></ItemTemplate>
<asp:TemplateField HeaderText="Title">
<ItemTemplate><asp:Label ID="lblttl" runat="server" Text='<%# Eval("ttl") %>'></asp:Label></ItemTemplate>
<asp:TemplateField HeaderText="When">
<ItemTemplate><asp:Label ID="lblwhen" runat="server" Text='<%# Eval("ewhen") %>'></asp:Label></ItemTemplate>
<asp:TemplateField HeaderText="Where">
<ItemTemplate><asp:Label ID="lblwhere" runat="server" Text='<%# Eval("ewhere") %>'></asp:Label></ItemTemplate>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />

In the above GridView i have used label control to display the data - in this case if i like to get the value of serial number ("sno" as in the above gridview) on selected row - I have to write the following code in codebehind file:

Protected Sub GridViewCalEvents_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridViewCalEvents.SelectedIndexChanged

Dim lblid As Label = DirectCast(row.FindControl("id"), Label)
MsgBox(lblid.Text, MsgBoxStyle.OkOnly)

End Sub

In the above code: I have used FindControl function to find the control named "id" and its Object Type as "label"
and store it in a variable named lblid

Hope it helps,


Shreesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response