ASP.NET Gridview使用更多数据绑定加载更慢

时间:2012-01-06 16:46:57

标签: asp.net gridview

我正在使用Visual Studio 2010和SQL Server 2008并使用sqldatasoure填充我的gridview。在gridview的itemtemplate中,有一些下拉列表再次与sqldatasource绑定,并在rowdatabound事件上为这些下拉列表分配选定的值。有近2000多条记录使得网格加载速度非常慢。任何优化这个的想法。

  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="Gridview1" runat="server" DataSourceID="SqlDataSource1" 
      AutoGenerateColumns="False" onrowdatabound="Gridview1_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="Dropdownlist1" runat="server" DataSourceID="SqlDataSource2" DataTextField="empid" DataValueField="empname">
                    </asp:DropDownList>
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("empid") %>' />
                    <asp:DropDownList ID="Dropdownlist2" runat="server" DataSourceID="SqlDataSource3" DataTextField="desgid" DataValueField="desgname">
                    </asp:DropDownList>
                    <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Eval("desgid") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$     ConnectionStrings:dbconn %>'
    SelectCommandType="StoredProcedure" SelectCommand="SP_Fetch_Grid">    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString='<%$ ConnectionStrings:dbconn %>'
    SelectCommandType="StoredProcedure" SelectCommand="SP_Fetch_DDL1"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString='<%$ ConnectionStrings:dbconn %>'
    SelectCommandType="StoredProcedure" SelectCommand="SP_Fetch_DDL2"></asp:SqlDataSource>
</div>
</form>

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HiddenField hf1 = (HiddenField)e.Row.FindControl("HiddenField1");
        HiddenField hf2 = (HiddenField)e.Row.FindControl("HiddenField2");
        DropDownList ddl1 = (DropDownList)e.Row.FindControl("DropDownList1");
        DropDownList ddl2 = (DropDownList)e.Row.FindControl("DropDownList2");
        ddl1.SelectedValue = hf1.Value;
        ddl2.SelectedValue = hf2.Value;
    }
}

3 个答案:

答案 0 :(得分:3)

  1. 一次不要显示2000行任何内容
  2. GridView上设置paging
  3. 如果前两项没有帮助,请考虑转而使用Repeater控件。
  4. 验证您的SQL查询是否在合理的时间范围内(2-3秒)返回您的数据。

答案 1 :(得分:1)

将下拉列表集一次拉入内存,然后绑定。这将为您节省2000多次服务器往返。

编辑 - 创建一个小类,如下所示:

public static class DropDownListCache
{
    private static Func<DataTable> m_getDataFunc = 
            () => AccessYourDAL.GetYourDropdownListTableData1();
    private static Func<DataTable> m_getDataFunc = 
            () => AccessYourDAL.GetYourDropdownListTableData2();

    private static Lazy<DataTable> DropDown1 = 
            new Lazy<TDataType>(m_getDataFunc1,true);
    private static Lazy<DataTable> DropDown2 = 
            new Lazy<TDataType>(m_getDataFunc2,true);

    public static DataTable GetDropDownList1()
    {
        return DropDown1;
    }

    public static DataTable GetDropDownList2()
    {
        return DropDown2;
    }
}

然后用对象数据源对象替换SqlDataSource2SqlDataSource3,这些对象指向缓存对象上的两个方法。

您需要编写AccessYourDAL.GetYourDropdownListTableData1();AccessYourDAL.GetYourDropdownListTableData2();

的代码

这些只是占位符。这应该确保您只需为整个网格支付2次往返,而不是每行2次。

1 + n * 2个数据库调用变为1 + 2个数据库调用。

答案 2 :(得分:0)

一种替代方法是通过在用于填充DropDownList的列上添加索引来优化sql查询。