使用 gridview asp.net 进行排序和分页
我试图让 gridview 手动排序和分页,但没有成功。
问题是,当用户单击他们想要排序的列时,它会对该页面进行排序,但不会对 gridview 后面的数据源(dataview)进行排序。 因此,当他们进入不同的页面时,他们的排序就会丢失。 我几乎正在寻找一种能够真正对 gridview 后面的数据源进行排序的排序。 这是我到目前为止所得到的:
protected void GridView_OnSort(object sender, GridViewSortEventArgs e)
{
String sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
DataView myDataView = new DataView(mybll.GetItemsOrdered());
myDataView.Sort = sortExpression + " DESC";
GridView.DataSource = myDataView;
GridView.DataBind();
}
else
{
DataView myDataView = new DataView(mybll.GetItemsOrdered());
myDataView.Sort = sortExpression + " ASC";
GridView.DataSource = myDataView;
GridView.DataBind();
}
}
任何帮助将不胜感激。 谢谢。
I'm trying to get a gridview to sort and page manually with no success.
The problem is that when a user clicks the column they want to sort, it sorts that page, but doesn't sort the datasource (dataview) behind the gridview. So when they progress to a different page, their sort is lost. Pretty much I'm looking for a sort that will actually sort the datasource behind the gridview. Here is what I have so far:
protected void GridView_OnSort(object sender, GridViewSortEventArgs e)
{
String sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
DataView myDataView = new DataView(mybll.GetItemsOrdered());
myDataView.Sort = sortExpression + " DESC";
GridView.DataSource = myDataView;
GridView.DataBind();
}
else
{
DataView myDataView = new DataView(mybll.GetItemsOrdered());
myDataView.Sort = sortExpression + " ASC";
GridView.DataSource = myDataView;
GridView.DataBind();
}
}
Any help would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将排序顺序保存在 ViewState 中。
为什么您不想使用现有的排序功能? 您可以随时自定义它。
MSDN 上的 对 GridView Web 服务器控件中的数据进行排序
以下是一个示例自定义:
http://www.netomatix.com/development/GridViewSorting.aspx
Save your sorting order in a ViewState.
Why you don't want to use existing sorting functionality? You can always customize it.
Sorting Data in a GridView Web Server Control at MSDN
Here is an example with customization:
http://www.netomatix.com/development/GridViewSorting.aspx
背后代码:
Code behind:
塔库斯的回答很有效。 不过,我建议用 SESSION 替换 VIEWSTATE。
当前页面的 VIEWSTATE 仅在当前页面回发到自身时起作用,并且在用户重定向到另一个页面后消失。 SESSION 不仅在当前页面的回发上保留排序顺序。 它在整个会话期间持续存在。 这意味着用户可以浏览其他页面,并且当他返回给定页面时,他上次使用的排序顺序仍然保留。 这通常更方便。
还有其他方法,例如保留用户配置文件。
我推荐这篇文章,它对 ViewState 及其如何与网页生命周期配合使用进行了很好的解释:https://msdn.microsoft.com/en-us/library/ms972976.aspx
要了解 VIEWSTATE、SESSION 和其他持久变量方式之间的区别,我推荐这篇文章:https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
Tarkus's answer works well. However, I would suggest replacing VIEWSTATE with SESSION.
The current page's VIEWSTATE only works while the current page posts back to itself and is gone once the user is redirected away to another page. SESSION persists the sort order on more than just the current page's post-back. It persists it across the entire duration of the session. This means that the user can surf around to other pages, and when he comes back to the given page, the sort order he last used still remains. This is usually more convenient.
There are other methods, too, such as persisting user profiles.
I recommend this article for a very good explanation of ViewState and how it works with a web page's life cycle: https://msdn.microsoft.com/en-us/library/ms972976.aspx
To understand the difference between VIEWSTATE, SESSION and other ways of persisting variables, I recommend this article: https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
我找到了一种更简单的方法,它允许您仍然使用标准 gridview 的内置排序/分页...
创建 2 个标签。 将它们设置为可见= false。 我调用了我的 lblSort1 和 lblSortDirection1,
然后编写了 2 个简单事件...页面排序,写入不可见标签的文本,以及使用它们的页面索引更改...
这比使用全局变量有点草率,但我发现asp尤其是全局变量是不可靠的......
I found a much easier way, which allows you to still use the built in sorting/paging of the standard gridview...
create 2 labels. set them to be visible = false. I called mine lblSort1 and lblSortDirection1
then code 2 simple events... the page sorting, which writes to the text of the invisible labels, and the page index changing, which uses them...
this is a little sloppier than using global variables, but I've found with asp especially that global vars are, well, unreliable...
更简单的方法...:
More simple way...: