在此上下文中不存在嵌套子gridview名称

时间:2018-02-10 05:45:41

标签: c# asp.net

所以我创建了一个gridview,在其中我给了另一个gridview。

<asp:GridView ID="dgInstitute" runat="server" AutoGenerateColumns="False" CellPadding="0"
    CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
    Visible="true" UseAccessibleHeader="true" OnRowCreated="dgInstitute_RowCreated"
    OnDataBound="dgInstitute_DataBound" OnRowDataBound="dgInstitute_RowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
        <asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default">
            <ItemTemplate>

                <asp:GridView ID="dgProgram" runat="server" AutoGenerateColumns="False" CellPadding="0"
                    CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
                    Visible="true" UseAccessibleHeader="true" OnRowCreated="dgProgram_RowCreated" OnDataBound="dgProgram_DataBound">
                    <Columns>
                        <asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
                        <asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default" ShowHeader="false">
                            <ItemTemplate>
                                <asp:Button AutoPostBack="true" Width="300px" CssClass="btn btn-primary btn-embossed" ID="btnProgram" runat="server"
                                    Text='<%#Eval("refValues") %>'
                                    OnClick="btnProgram_Click" CommandArgument='<%#Eval("refGroupId") %>' />

                                <div class="voffset3"></div>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle CssClass="panel-default"></HeaderStyle>
                    <RowStyle CssClass=""></RowStyle>
                </asp:GridView>

                <div class="voffset3"></div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle CssClass="panel-default"></HeaderStyle>
    <RowStyle CssClass=""></RowStyle>
</asp:GridView>

为此第一个gridview可以在后面的代码中访问,但是当我尝试使用secondGridview时,它表示dgProgram name doe在当前上下文中不存在。

我重新启动了我的解决方案,但没有用。

1 个答案:

答案 0 :(得分:0)

您需要通过行索引在父GridView上使用FindControl。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        //bind the datasource for the parent gridview
        dgInstitute.DataSource = mySource1;
        dgInstitute.DataBind();

        //find the nested gridview in row 4 and cast it
        GridView nestedGridView = dgInstitute.Rows[3].FindControl("dgProgram") as GridView;

        //bind the datasource for the nested gridview
        nestedGridView.DataSource = mySource2;
        nestedGridView.DataBind();
    }
}

或者在父GridView的RowDataBound事件中。

protected void dgInstitute_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the nested gridview in row 4 and cast it
        GridView nestedGridView = e.Row.FindControl("dgProgram") as GridView;

        //bind the datasource for the nested gridview
        nestedGridView.DataSource = mySource2;
        nestedGridView.DataBind();
    }
}