本文介绍了在的FindControl GridView控件返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前处于两难境地与我的GridView不返回一个标签,这是一个DetailsView内...

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

我的C#code是:

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的:

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>

当我断点

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

标签出来为(numberLbl)...

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

从异常返回的信息是:
对象引用不设置到对象的实例

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

编辑:
这似乎可以解决,如果我在外部GridView控件生成lblNumber(在页面上)用的eval(数字),虽然我不明白为什么它不会在当前的GridView中,我试图与合作工作,给予这是GridView1一个DetailsView内。

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.

推荐答案

使用的FindControl时,不应使用细胞采集。只要使用此

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");

这篇关于在的FindControl GridView控件返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:51