使用更改页面的C#问题在ASP.Net中嵌套GridViews

时间:2019-06-19 09:36:22

标签: c# asp.net gridview nested-gridview

我正在尝试为单个父行打开nested gridview,并正确打开子行的单个记录。

在主gridview中提供了行的分页。

我的问题是页面更改,当我尝试更改页面时出现此错误:

  

GridView分页事件gvProducts_PageIndexChanging无法触发

该如何解决?

我的下面的代码。

在此先感谢您的帮助。

代码标记

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" CssClass="Grid"
    DataKeyNames="CustomerID" OnRowDataBound="gvProducts_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img alt = "" style="cursor: pointer" src="images/plus.png" />
                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>

    <PagerTemplate>
       <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back_doppio.gif"
                        CommandArgument="First" CommandName="Page" />
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
                        CommandArgument="Prev" CommandName="Page" />
         Page
         <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
                            OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">
         </asp:DropDownList>
          of
         <asp:Label ID="lblPageCount" runat="server"></asp:Label>
         <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
                        CommandArgument="Next" CommandName="Page" />
         <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next_doppio.gif"
                        CommandArgument="Last" CommandName="Page" />
    </PagerTemplate>

</asp:GridView>

隐藏代码

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string customerId = gvProducts.DataKeys[e.Row.RowIndex].Value.ToString();

        sql = @String.Format(" SELECT ... ");

        GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
        gvOrders.DataSource = GetData(sql);
        gvOrders.DataBind();
    }


    if (e.Row.RowType == DataControlRowType.Pager)
    {
        DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
        Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

        if (lblPageCount != null)
            lblPageCount.Text = gvProducts.PageCount.ToString();

        for (int i = 1; i <= gvProducts.PageCount; i++)
        {
            ddl.Items.Add(i.ToString());
        }

        ddl.SelectedIndex = gvProducts.PageIndex;

        if (gvProducts.PageIndex == 0)
        {
            ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
        }

        if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
        {
            ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
        }
    }
}


protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvrPager = gvProducts.BottomPagerRow;
    DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
    gvProducts.PageIndex = ddlPages.SelectedIndex;
    BindData();
}

protected void Paginate(object sender, CommandEventArgs e)
{
    int intCurIndex = gvProducts.PageIndex;

    switch (e.CommandArgument.ToString().ToLower())
    {
        case "First":
            gvProducts.PageIndex = 0;
            break;
        case "Prev":
            gvProducts.PageIndex = intCurIndex - 1;
            break;
        case "Next":
            gvProducts.PageIndex = intCurIndex + 1;
            break;
        case "Last":
            gvProducts.PageIndex = gvProducts.PageCount - 1;
            break;
    }
    gvProducts.DataBind();
}

protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvProducts.PageIndex = e.NewPageIndex;
    BindData();
}

1 个答案:

答案 0 :(得分:1)