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"
GridLines="Vertical">
<RowStyle BackColor="#F7F7DE" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%# Eval("sno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate><asp:Label ID="lbldtp" runat="server" Text='<%# Eval("dtp") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate><asp:Label ID="lblttl" runat="server" Text='<%# Eval("ttl") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="When">
<ItemTemplate><asp:Label ID="lblwhen" runat="server" Text='<%# Eval("ewhen") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Where">
<ItemTemplate><asp:Label ID="lblwhere" runat="server" Text='<%# Eval("ewhere") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
<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" />
</asp:GridView>
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,
Regards,
Thiru.
Shreesh, if this helps please login to Mark As Answer. | Alert Moderator