简体   繁体   中英

can't find a control inside my gridview

I have a simple gridview that contains a label in one of the rows. I'm trying to access that label in the RowDataBound event, but for some reason I keep getting a "Object reference not set to an instance of an object." error on the line where I am using FindControl.

I've tried using "gvQReport.FindControl", "e.Row.FindControl", and "Me.FindControl" but nothing works.

Am I not doing this correctly?

Thanks!

    Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
         Dim lblTest As Label = CType(gvQReport.FindControl("lblTest"), Label)
         lblTest.Text = "test Label"
    End Sub


<asp:GridView ID="gvQReport" OnRowDataBound="gvQReport_RowDataBound" runat="server">
     <Columns>
            <asp:TemplateField HeaderText="Test">
                <ItemTemplate>
                    <asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
     </Columns>
</asp:GridView>

The Row property of GridViewRowEventArgs is the current row, look for your control there instead of the whole GridView .

Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
     If e.Row.RowType = DataControlRowType.DataRow Then
         Dim lblTest As Label = CType(e.Row.FindControl("lblTest"), Label)
         lblTest.Text = "test Label"
     End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM