I have a blank Repeater nested inside a column in a GridView and want to use a button in the GridView to populate the Repeater on demand via code behind. I am having difficulties referencing the repeater inside my button’s onCommand sub.
Here is the relevant markup:
<asp:GridView ID="Submission" runat="server" AllowPaging="true" AllowSorting="true" />
<Columns>
..........
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="AdminEditSubmission" runat="server" ImageUrl="edit_15.png"
alt="" OnCommand="loadDetails" CommandArgument="X" />
</ItemTemplate>
</asp:TemplateField>
..........
<asp:TemplateField>
<ItemTemplate>
<asp:Repeater ID="RptSubmissionDetail" runat="server">
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For the sake of this small example, I will simply try to update the repeater’s header with “Hello world”. However, when it gets to the last line, I get the dreaded “Object reference not set to an instance of an object” error.
Sub loadDetails(sender As Object, e As CommandEventArgs)
Dim rpt As Repeater = CType(Page.FindControl("RptsSubmissionDetail"), Repeater)
Dim tmpHdr As TemplateBuilder = New TemplateBuilder
tmpHdr.AppendLiteralString("Hello World")
rpt.HeaderTemplate = tmpHdr
End Sub
Can anyone tell me how to reference this repeater from my ImageButton click sub? I have tried several, with Page.FindControl(“RptsSubmissionDetail”) only being my latest attempt.
In GridView’s RowCommand you need to use
FindControlon theGridViewRow:From the ImageButton’s click event it’s nearly the same. Cast the
senderargument to theImageButtonand it’sNamingContainerproperty to theGridViewRow. Then useFindControlas shown above: