0

I am currently in a dilemma with my gridview not returning a label, which is within a detailsview...

My C# code is:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    // get pet number for when removing a pet from reservation
    int numberSelected = -1;
    String numbertxt = "-1";

    GridView gv1 = (GridView)sender;
    GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];
    Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");

    // find selected index, and get number in column 0
    // label within GridView1 within dvReservation DetailsView
    numbertxt = numberLbl.Text;
    ...

Gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="dsObjGet" 
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField InsertVisible="False" ShowHeader="False">
            <AlternatingItemTemplate>
                <asp:Label ID="lblNumber" runat="server"
                    Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
            </AlternatingItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblNumber" runat="server" 
                    Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <AlternatingItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
                </asp:Label>
            </AlternatingItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField SelectText="Remove" ShowSelectButton="True" 
            CausesValidation="False">
            <ControlStyle CssClass="link" />
        </asp:CommandField>
    </Columns>
</asp:GridView>

When I breakpoint

Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");

the label comes out as null (numberLbl)...

The message returned from the exception is: "Object reference not set to an instance of an object"

EDIT: This seems to be resolved if I generate lblNumber in an external gridview (on the page) with Eval("NUMBER"), though I don't see why it doesn't work in the current GridView I was trying to work with, given that GridView1 is within a DetailsView.

user3554331
  • 3
  • 1
  • 4

1 Answers1

0

You should not use the Cell Collection when using FindControl. Just use this

GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.SelectedRow;
Label numberLbl = (Label)rvRow.FindControl("lblNumber");
naveen
  • 53,448
  • 46
  • 161
  • 251
  • I have tried your answer and [another here.](http://forums.asp.net/t/1001099.aspx?Finding+a+control+in+GridView+in+EDIT+Mode) None works... Infact getting the exception for `Index was out of range. Must be non-negative and less than the size of the collection.` This is rather weird - because I have 10 rows in the grid and I only click on 2nd or 3rd row, yet it's going out of bound...what do you think is happening? – bonCodigo Jul 16 '14 at 02:20
  • @bonCodigo: can you post it as a question in SO? Show me your markup and event in which you want the control. – naveen Jul 16 '14 at 10:13
  • I finally got it fixed. [Please check here. It is really nothing to do with `FindControl`, it was something else that was screwed up...](http://stackoverflow.com/a/24777781/1389394) – bonCodigo Jul 16 '14 at 10:16