简体   繁体   English

如果从后面的代码中分配了select命令,则SqlDataSource如何工作?

[英]How does SqlDataSource work if select command is assigned from code behind?

I am binding a gridview using SqlDataSource and I am assigning the SQL select command from code behind to SqlDataSource . 我正在使用SqlDataSource绑定gridview,并将SQL select命令从后面的代码分配给SqlDataSource。 I have also enabled pagination in my GridView with page size 25. 我还在页面大小为25的GridView中启用了分页。

Now what I want to know is if I am assigning query from code behind then does SqlDataSource takes care of pagination. 现在我想知道的是,如果我要从后面的代码分配查询,那么SqlDataSource是否会处理分页。 I mean it should fetch 25 records at a time based on the selected page number. 我的意思是,它应该根据所选的页码一次获取25条记录。

I am concerned because my GridViewis showing data too slow and I feel that SqlDataSource is fetching all 9174 records (result set of my query) and displaying first 25 of them in first page. 我很担心,因为我的GridView显示数据的速度太慢,而且我觉得SqlDataSource正在获取所有9174条记录(查询的结果集),并在第一页中显示其中的前25条记录。 When I switch to second page it looks like it is fetching all 9174 records again and showing next 25 records. 当我切换到第二页时,它似乎正在再次获取所有9174条记录并显示接下来的25条记录。

Can anyone help me to understand if what I am assuming is correct or the flow in my case is different. 谁能帮助我了解我的假设是否正确,或者我的处理流程是否不同。

yes you are correct. 是的,你是对的。 It gets all the records and then get 25 records for specified page index. 它获取所有记录,然后获取指定页面索引的25条记录。 If you feel it is inefficient, then you have to manually do that. 如果您觉得效率低下,则必须手动执行。 First get count of records(In your case 9174) then 9174/25 is the number of pages. 首先获取记录计数(在您的情况下为9174),然后9174/25为页数。 when loading grid get only first 25 record and display it in grid. 加载网格时,仅获得前25条记录并将其显示在网格中。 Also manually add next and previous buttons to navigate. 还可以手动添加下一个和上一个按钮进行导航。 According to current page(lets say 6) if next page clicked(want to get the 7 page data 151 record to 175 record) then get that 25 records and bind it in next button click. 根据当前页面(假设为6),如果单击了下一页(想要将7页数据151条记录转换为175条记录),则获得该25条记录并将其绑定到下一步按钮中。 Like this you should have to manually do that by maintaining the current page number. 这样,您应该必须通过维护当前页码来手动执行此操作。

If you are doing it be setting datasource in the Selecting event: 如果要执行此操作,请在“选择”事件中设置数据源:

protected void MyDataSourceSelecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = SomeIqueryableValue;
}

Then the grid will handle paging automatically using the IQueryable deferred execution. 然后,网格将使用IQueryable延迟执行自动处理分页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM