如何使用带有TemplateFields的ObjectDataSource对GridView进行排序

Jon*_*hem 7 asp.net gridview objectdatasource templatefield

背景:

我正在使用GridView和ObjectDataSource.我正在实施分页和排序.

在ObjectDataSource上:

        objectDataSource.TypeName = value;
        objectDataSource.SelectMethod = "Select";
        objectDataSource.SelectCountMethod = "SelectCount";
        objectDataSource.SortParameterName = "sortExpression";
        objectDataSource.EnablePaging = true;
Run Code Online (Sandbox Code Playgroud)

在GridView上:

        gridView.AllowPaging = true;
        gridView.AllowSorting = true;
        gridView.DataSource = objectDataSource;
Run Code Online (Sandbox Code Playgroud)

为了使分页和排序工作,我将"EnableSortingAndPagingCallbacks"设置为True.之前,我得到一个"System.Web.HttpException:GridView触发的事件排序,但没有处理." 这解决了它.

如果我在GridView中只使用BoundFields,这很好并且工作正常.

但是,如果我使用TemplateFields,我会收到"NotSupportedException:TemplateField不支持回调,因为某些控件无法在回调中正确更新.在GridView上关闭回调."

哪个,有道理.我只需要知道如何在不使用EnableSortingAndPagingCallbacks的情况下进行排序.

如果EnableSortingAndPagingCallbacks = True:

  • 寻呼作品
  • 排序工作
  • BoundFields工作
  • 模板列做工作

如果EnableSortingAndPagingCallbacks = False:

  • 寻呼作品
  • 排序并没有工作
  • BoundFields工作
  • TemplateFields工作

我的问题:

如何让Paging,Sorting和TemplateField同时工作?


澄清实施情况:

将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;
    }
Run Code Online (Sandbox Code Playgroud)

特定的SortExpression在aspx/ascx中定义:

<Columns>
       <asp:BoundField HeaderText="County Name" DataField="CountyName" SortExpression="Name" />
       <asp:BoundField HeaderText="State Name" DataField="StateName" SortExpression="State.Name" />
</Columns>
Run Code Online (Sandbox Code Playgroud)

应该被传入并在单击列时调用ObjectDataSource上的Select方法,但是如果EnableSortingAndPagingCallbacks = true则它似乎不起作用,而是我得到关于未定义Sorting事件的异常.

Zen*_*sar 0

属性 EnableSortingAndPagingCallbacks 告诉控件对数据进行客户端排序,以便控件看起来自动排序而无需页面回发。此方法不支持 TemplateFields。为了使用TemplateFields 并执行排序,您需要连接GridView.Sorting 事件,并将AllowSorting 属性设置为true。完成后,当单击列标题时应该触发该事件,并且可以从那里处理排序逻辑。