如何使用 ObjectDataSource 和 TemplateFields 在 GridView 上排序
背景:
我正在使用 GridView 和 ObjectDataSource。 我正在实现分页和排序。
在 ObjectDataSource 上:
objectDataSource.TypeName = value;
objectDataSource.SelectMethod = "Select";
objectDataSource.SelectCountMethod = "SelectCount";
objectDataSource.SortParameterName = "sortExpression";
objectDataSource.EnablePaging = true;
在 GridView 上:
gridView.AllowPaging = true;
gridView.AllowSorting = true;
gridView.DataSource = objectDataSource;
为了使分页和排序正常工作,我将“EnableSortingAndPagingCallbacks”设置为 True。 之前,我收到“System.Web.HttpException:GridView 触发了未处理的事件排序”。 这解决了它。
如果我在 GridView 中仅使用 BoundFields,这非常棒并且工作正常。
但是,如果我使用 TemplateFields,则会收到“NotSupportedException:TemplateField 不支持回调,因为某些控件无法在回调中正确更新。在 GridView 上关闭回调。”
这是有道理的。 我只需要知道如何进行排序工作,而不使用 EnableSortingAndPagingCallbacks。
如果 EnableSortingAndPagingCallbacks = True:
- 分页工作
- 排序工作
- BoundFields 工作
- TemplateFields 不工作
如果 EnableSortingAndPagingCallbacks = False:
- 分页工作
- 排序不工作
- BoundFields 工作
- TemplateFields 工作
我的问题:
我如何让分页、排序和 TemplateFields 工作,所有这些都在同时?
实现说明:
将 ObjectDataSource 与 GridView 一起使用需要实现一个名为 Select 的方法,该方法提供排序表达式、要返回的行数和起始行:
public IEnumerable<CountyAndStateGridRow> Select(string sortExpression, int maximumRows, int startRowIndex)
{
string oql = "select County order by {" + sortExpression + "}" ;
var counties = QueryProvider.ExecuteQuery(oql).Cast<County>();
var page = counties.Skip(startRowIndex).Take(maximumRows);
var rows = page.Select(
county => new CountyAndStateGridRow
{
CountyName = county.Name,
StateName = county.State.Name,
});
return rows;
}
具体的 SortExpression 在 aspx 中定义/ascx:
<Columns>
<asp:BoundField HeaderText="County Name" DataField="CountyName" SortExpression="Name" />
<asp:BoundField HeaderText="State Name" DataField="StateName" SortExpression="State.Name" />
</Columns>
这是应该被传入并在单击列时调用ObjectDataSource上的Select方法,但如果EnableSortingAndPagingCallbacks = true,它似乎不起作用,相反,我得到了有关的异常未定义排序事件。
Background:
I am working with a GridView and an ObjectDataSource. I am implementing Paging and Sorting.
On the ObjectDataSource:
objectDataSource.TypeName = value;
objectDataSource.SelectMethod = "Select";
objectDataSource.SelectCountMethod = "SelectCount";
objectDataSource.SortParameterName = "sortExpression";
objectDataSource.EnablePaging = true;
On the GridView:
gridView.AllowPaging = true;
gridView.AllowSorting = true;
gridView.DataSource = objectDataSource;
To get paging and sorting to work, I set "EnableSortingAndPagingCallbacks" to True. Before, I was getting a "System.Web.HttpException: The GridView fired event Sorting which wasn't handled." and this fixes it.
If I use only BoundFields in my GridView, this is great and works fine.
However, if I used TemplateFields, I get a "NotSupportedException: Callbacks are not supported on TemplateField because some controls cannot update properly in a callback. Turn callbacks off on GridView."
Which, makes sense. I just need to know how to make sorting work, without using EnableSortingAndPagingCallbacks.
If EnableSortingAndPagingCallbacks = True:
- Paging Works
- Sorting Works
- BoundFields Work
- TemplateFields do Not Work
If EnableSortingAndPagingCallbacks = False:
- Paging Works
- Sorting does Not Work
- BoundFields Work
- TemplateFields Work
My Question:
How do I go about getting Paging, Sorting, and TemplateFields to work, all at the same time?
Clarification on the implementation:
Using an ObjectDataSource with a GridView requires implementing a method called Select that provides a sort expression, the number of rows to return, and the start row:
public IEnumerable<CountyAndStateGridRow> Select(string sortExpression, int maximumRows, int startRowIndex)
{
string oql = "select County order by {" + sortExpression + "}" ;
var counties = QueryProvider.ExecuteQuery(oql).Cast<County>();
var page = counties.Skip(startRowIndex).Take(maximumRows);
var rows = page.Select(
county => new CountyAndStateGridRow
{
CountyName = county.Name,
StateName = county.State.Name,
});
return rows;
}
The specific SortExpression is defined in the aspx/ascx:
<Columns>
<asp:BoundField HeaderText="County Name" DataField="CountyName" SortExpression="Name" />
<asp:BoundField HeaderText="State Name" DataField="StateName" SortExpression="State.Name" />
</Columns>
This is supposed to be passed in and call the Select method on the ObjectDataSource when the column is clicked, but it does not seem to work if EnableSortingAndPagingCallbacks = true, and instead I get the exception about the Sorting event not being defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使排序功能正常工作:
在 .cs 文件中,您需要编写
For sorting functionality to work:
in .cs file you need to write
属性 EnableSortingAndPagingCallbacks 告诉控件对数据进行客户端排序,以便控件看起来自动排序而无需页面回发。 此方法不支持 TemplateFields。 为了使用TemplateFields 并执行排序,您需要连接GridView.Sorting 事件,并将AllowSorting 属性设置为true。 完成后,当单击列标题时应该触发该事件,并且可以从那里处理排序逻辑。
The property EnableSortingAndPagingCallbacks tells the control to do a client side sort of the data, so that the control appears to automatically sort without a page postback. TemplateFields are not supported with this method. In order to use TemplateFields and perform sorting, you need to wire up the GridView.Sorting event, and set the AllowSorting property to true. Once that is done, the event should fire when the column header is clicked and the sorting logic can be handled from there.
将 SortExpression 值更改为 DataField 的值。
将AllowPaging 和AllowSorting 设置为true。
将 EnableSortingAndPagingCallbacks 设置为 true。
Change the SortExpression value with the value of DataField.
Set AllowPaging and AllowSorting to true.
Set EnableSortingAndPagingCallbacks to true.