ASP.NET Gridview加载速度较慢,数据绑定越多

ASP.NET Gridview加载速度较慢,数据绑定越多,asp.net,gridview,Asp.net,Gridview,我正在使用Visual Studio 2010和SQL Server 2008,并用sqldatasource填充我的gridview。在gridview的itemtemplate中有一些dropdownlist,它们再次与sqldatasource绑定,在rowdatabound事件中,我为这些dropdownlist指定了选定的值。有将近2000多条记录,这使得网格加载速度非常慢。有没有优化这个的想法 <form id="form1" runat="server"> &l

我正在使用Visual Studio 2010和SQL Server 2008,并用sqldatasource填充我的gridview。在gridview的itemtemplate中有一些dropdownlist,它们再次与sqldatasource绑定,在rowdatabound事件中,我为这些dropdownlist指定了选定的值。有将近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;
    }
}

受保护的void Gridview1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
HiddenField hf1=(HiddenField)e.Row.FindControl(“HiddenField 1”);
HiddenField hf2=(HiddenField)e.Row.FindControl(“HiddenField 2”);
DropDownList ddl1=(DropDownList)e.Row.FindControl(“DropDownList1”);
DropDownList ddl2=(DropDownList)e.Row.FindControl(“DropDownList2”);
ddl1.SelectedValue=hf1.Value;
ddl2.SelectedValue=hf2.Value;
}
}
  • 不要一次显示2000行任何内容
  • GridView
    上设置
  • 如果前两项不起作用,请考虑切换到控件。
  • 验证SQL查询是否在合理的时间范围(2-3秒)内返回数据
  • 不要一次显示2000行任何内容
  • GridView
    上设置
  • 如果前两项不起作用,请考虑切换到控件。
  • 验证SQL查询是否在合理的时间范围(2-3秒)内返回数据

  • 将下拉列表集拉入内存一次,并绑定到该列表。这将为您节省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;
        }
    }
    
    公共静态类DropDownListCache
    {
    私有静态函数m_getDataFunc=
    ()=>AccessYourDAL.GetYourDropdownListTableData1();
    私有静态函数m_getDataFunc=
    ()=>AccessYourDAL.GetYourDropdownListTableData2();
    私有静态惰性下拉列表1=
    新的Lazy(m_getDataFunc1,true);
    私有静态惰性下拉列表2=
    新的Lazy(m_getDataFunc2,true);
    公共静态数据表GetDropDownList1()
    {
    返回下拉列表1;
    }
    公共静态数据表GetDropDownList2()
    {
    返回下拉列表2;
    }
    }
    
    然后将
    SqlDataSource2
    SqlDataSource3
    替换为对象数据源对象,这些对象指向缓存对象上的两个方法

    您需要为
    AccessYourDAL.GetYourDropdownListTableData1()编写代码
    AccessYourDAL.GetYourDropdownListTableData2()

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


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

    将下拉列表集拉入内存一次,然后绑定到内存中。这将为您节省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;
        }
    }
    
    公共静态类DropDownListCache
    {
    私有静态函数m_getDataFunc=
    ()=>AccessYourDAL.GetYourDropdownListTableData1();
    私有静态函数m_getDataFunc=
    ()=>AccessYourDAL.GetYourDropdownListTableData2();
    私有静态惰性下拉列表1=
    新的Lazy(m_getDataFunc1,true);
    私有静态惰性下拉列表2=
    新的Lazy(m_getDataFunc2,true);
    公共静态数据表GetDropDownList1()
    {
    返回下拉列表1;
    }
    公共静态数据表GetDropDownList2()
    {
    返回下拉列表2;
    }
    }
    
    然后将
    SqlDataSource2
    SqlDataSource3
    替换为对象数据源对象,这些对象指向缓存对象上的两个方法

    您需要为
    AccessYourDAL.GetYourDropdownListTableData1()编写代码
    AccessYourDAL.GetYourDropdownListTableData2()

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


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

    一个备选方案是通过在用于填充DropDownList的列上添加索引来优化sql查询。

    一个备选方案是通过在用于填充DropDownList的列上添加索引来优化sql查询。

    以不同的方式设计UI。就可用性而言,+2000条记录的ddl不是最佳实践。@aleafonso我认为OP意味着GridView中有2000条记录。需要从列表中为每一行选择值。我想我找不到其他替代方案,我会建议用不同的方式设计UI。就可用性而言,+2000条记录的ddl不是最佳实践。@aleafonso我认为OP意味着GridView中有2000条记录。需要从列表中为每一行选择值。我想我找不到其他的选择了。谢谢。SQL查询以秒为单位显示记录。2.我使用的是客户端过滤器,所以当使用分页时,它只过滤那些分页的行。1.我从来没有使用过repeater控件,我用谷歌搜索它,如果你有任何工作代码,请发到这里。@M.Bharnidharan,我编辑了我的答案,在
    repeater
    控件上添加了指向MSDN帮助文章的链接——页面底部有一个例子。您应该知道,它非常基本,不包括
    GridView
    控件所具有的许多特性(如内置分页)。我建议,根据您的评论,如果可能的话,您也可以考虑使用服务器端过滤器。您还可以查看一些jQuery/JavaScript网格选项,