0

我有三个网格 - 类别网格、项目网格和子项目网格。我有类别网格(grdCategories),对于每个类别,我都有许多项目绑定到子网格(grdItems)。对于每个项目,我在子子网格(grdSubItems)中显示了许多子项目。现在的问题是该页面在加载数据时花费了太多时间。我的 HTML 代码如下:

加载数据甚至需要几分钟:

ASPX

   <asp:GridView ID="grdCategories" runat="server" AutoGenerateColumns="false" DataKeyNames="Category"
                    CssClass="menu_items" OnRowDataBound="grdCategories_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Panel ID="pnlMealOptionsHeader" runat="server" Width="100%">
                                    <h1>
                                        <a id='<%#  Eval("CategoryX")%>'>
                                            <asp:Label ID="lblCategory" runat="server" Text='<%#Eval("Category") %>' Visible="false"></asp:Label>
                                            <asp:Label ID="lblCategoryX" runat="server" Text='<%#Eval("CategoryX") %>'></asp:Label>
                                        </a>
                                    </h1>
                                </asp:Panel>
                                <asp:Panel runat="server" ID="pnlMealOptionsBody">
                                    <asp:Label ID="lblCategoryXX" runat="server" CssClass="title_2" Text='<%#Eval("CategoryXX") %>'></asp:Label>
                                    <!--Items in Category -->
                                    <asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="false" CssClass="active-grid"
                                        DataKeyNames="Item" OnRowDataBound="grdItems_RowDataBound" ShowHeader="false" >
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:Label ID="lblItem" runat="server" Text='<%#Eval("Item") %>' Visible="false"></asp:Label>
                                                    <asp:Label ID="lblItemX" CssClass="title" runat="server" Text='<%#Eval("ItemX") %>'></asp:Label>
                                                    <asp:Label ID="lblItemXX" CssClass="title" runat="server" Text='<%#Eval("ItemXX") %>' style=" font-size:smaller; font-weight:normal"></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:GridView ID="grdSubItems" runat="server" AutoGenerateColumns="false" CssClass=""
                                                        DataKeyNames="SubItem" ShowHeader="false" OnRowDataBound="grdSubItems_RowDataBound">
                                                        <HeaderStyle HorizontalAlign="Left" />
                                                        <Columns>
                                                            <asp:TemplateField>
                                                                <ItemTemplate>
                                                                    <asp:Label ID="lblItem" runat="server" Text='<%#Eval("Item") %>' Visible="false"></asp:Label>
                                                                    <asp:Label ID="lblSubItem" runat="server" Text='<%#Eval("SubItem") %>' Visible="false"></asp:Label>
                                                                    <asp:Label ID="lblNoofOptions" runat="server" Text='<%#Eval("NumofOptions") %>' Visible="false"></asp:Label>
                                                                    <asp:Label ID="lblSubItemX" CssClass="qty" runat="server" Text='<%#Eval("SubItemX") %>'></asp:Label>
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:TemplateField>
                                                                <ItemTemplate>
                                                                    <asp:Label ID="lblPrice" runat="server" CssClass="price" Text='<%#String.Format("£{0}",Eval("SellingCost")) %>'></asp:Label>
                                                                </ItemTemplate>
                                                            </asp:TemplateField> 
                                                        </Columns>
                                                    </asp:GridView>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>
                                    <!--End Items in Category -->
                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <table width="900px">
                            <tr>
                                <td align="center">
                                    <h1>
                                        No Data Available</h1>
                                </td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                </asp:GridView>

代码背后

类别 Gridview 绑定

    private void FillCategoriesGrid()
    {
        DataSet ds = new DataSet();
        ShopCategoryMapBL bl = new ShopCategoryMapBL(SessionContext.SystemUser);
        bl.FetchForShop(ds, RowId);

        grdCategories.DataSource = ds.Tables[0].DefaultView;
        grdCategories.DataBind();
    }

项目 Gridview 绑定

    protected void grdCategories_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView chkTopings = e.Row.FindControl("grdItems") as GridView;
            Label lblCategory = e.Row.FindControl("lblCategory") as Label;

            FillItemsGrid(chkTopings, WebHelper.Cast(lblCategory.Text, 0));
        }
    }

    protected void FillItemsGrid(GridView grdItems, int Category)
    {
        try
        {
            //int cleanOrder = CargoBag.GetValue("CleanOrder", 0);
            DataSet aDataSet = new DataSet();
            ItemBL bl = new ItemBL(SessionContext.SystemUser);
            bl.FetchForCategory(aDataSet, Category, RowId);
            grdItems.DataSource = aDataSet.Tables[0];
            grdItems.DataBind();

        }
        catch (Exception ex) { }
    }

子项 Gridview 绑定

    protected void grdItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView grdSubItems = e.Row.FindControl("grdSubItems") as GridView;
            Label lblItem = e.Row.FindControl("lblItem") as Label;

            FillSubItemsGrid(grdSubItems, WebHelper.Cast(lblItem.Text, 0));
        }
    }


    protected void FillSubItemsGrid(GridView grdSubItems, int Item)
    {
        try
        {
            //int cleanOrder = CargoBag.GetValue("CleanOrder", 0);
            DataSet aDataSet = new DataSet();
            SubItemBL bl = new SubItemBL(SessionContext.SystemUser);
            bl.FetchForItem(aDataSet, Item);
            grdSubItems.DataSource = aDataSet.Tables[0];
            grdSubItems.DataBind();

        }
        catch (Exception ex) { }
    }
4

1 回答 1

0

您需要提供所有代码,我们才能提供准确的答案。

例如:

SubItemBL 在哪里定义?

还要尝试查看确切的瓶颈是哪条线。

是吗

bl.FetchForCategory(aDataSet, Category, RowId);

或者

grdSubItems.DataBind();

如果以上行是瓶颈,请注意 Gridview 绑定到具有大量数据的数据源确实很慢。你的数据有多大?

于 2013-08-16T11:30:09.630 回答