动态SELECT查询和带有SqlDataSource的GridView的问题 [英] Issues with dynamic SELECT query and GridView with SqlDataSource

查看:92
本文介绍了动态SELECT查询和带有SqlDataSource的GridView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据表,其中包含员工发布到公司网站上的文章.用户可以选择是否选择各种过滤器,例如作者和发布日期,然后按搜索按钮.结果显示在页面GridView上.

潜在的机制是,当单击按钮时,将使用过滤条件生成SELECT字符串.如果过滤器为空(例如,作者"文本框为空白),则不使用过滤器.网格的关联SqlDataSource控件的SelectParameters属性被清除并使用请求的过滤器重置,源的SelectCommand属性是通过新查询设置的,并且网格已绑定.该页面然后显示数据的第一页.一切都按预期工作.

当用户尝试导航到另一页数据时出现问题.有一个回发,并且没有重新加载数据,从而导致一个空的网格.我想要的行为是通过数据分页将通过数据分页.

我了解问题所在:数据在两次加载之间不持久.我怀疑有一种直接的方法可以解决此问题,但我还无法弄清楚.我已经看过重新加载数据并手动设置PageIndex的情况,但是我仍然无法弄清楚如何检索请求的页面索引.

有任何建议吗?

I have a large data table containing articles published by staff to the corporate website. Users can select or not select various filters such as author and publish date, and press the Search button. The results are displayed on a paged GridView.

The underlying mechanism is that when the button is clicked, a SELECT string is generated using the filter criteria. If a filter is empty (say, the "Author" textbox is left blank) then the filter is not used. The SelectParameters property of the grid''s associated SqlDataSource control is cleared and reset with the requested filters, the source''s SelectCommand property is set with the new query, and the grid is bound. The page then displays the first page of data. All working exactly as expected.

The problem comes up when the user tries to navigate to a different page of data. There is a postback and the data is not reloaded, resulting in an empty grid. The behavior I want is that paging through the data will page through the data.

I understand the problem: the data is not persisting between loads. I suspect there is a straightforward way to manage this, but I haven''t been able to figure that out yet. I have looked at reloading the data and manually setting PageIndex, but I haven''t been able to figure out how to retrieve the requested page index.

Any suggestions?

推荐答案

通常,这是通过保留过滤器并通过处理 PageIndexChanging [
Generally this is handled by persisting the filter and reapplying on postback by handling the PageIndexChanged[^] or PageIndexChanging[^] events


尝试:
1.启用GridView的分页
2.设置SQLDatasource的EnableCaching属性

请查看这篇文章以获取全部详细信息:
数据的自定义分页和缓存 [
Try:
1. Enable the pagination of the GridView
2. Set EnableCaching property of the SQLDatasource

Have a look at this article for full detail:
Custom Paging and Caching of Data[^]


这篇关于动态SELECT查询和带有SqlDataSource的GridView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆