如何使用ObjectDataSource控件与模板列进行排序的GridView [英] How to Sort on a GridView using ObjectDataSource with TemplateFields

查看:229
本文介绍了如何使用ObjectDataSource控件与模板列进行排序的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用GridView和一个ObjectDataSource工作。我实现分页和排序。

I am working with a GridView and an ObjectDataSource. I am implementing Paging and Sorting.

在ObjectDataSource控件:

On the ObjectDataSource:

        objectDataSource.TypeName = value;
        objectDataSource.SelectMethod = "Select";
        objectDataSource.SelectCountMethod = "SelectCount";
        objectDataSource.SortParameterName = "sortExpression";
        objectDataSource.EnablePaging = true;

在GridView的:

On the GridView:

        gridView.AllowPaging = true;
        gridView.AllowSorting = true;
        gridView.DataSource = objectDataSource;

要获得分页和排序工作,我设置EnableSortingAndPagingCallbacks为True。之前,我得到一个System.Web.HttpException:GridView控件触发的事件排序这是不处理的。而这个修复它。

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.

如果我只用在我的GridView控件绑定列,这是伟大的,工作正常。

If I use only BoundFields in my GridView, this is great and works fine.

不过,如果我使用模板列,我得到一个NotSupportedException异常:回调不支持的TemplateField因为某些控件不能在回调中正确更新打开关闭的回调GridView控件上。

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."

这是有道理的。我只需要知道如何分拣工作,而无需使用EnableSortingAndPagingCallbacks。

Which, makes sense. I just need to know how to make sorting work, without using EnableSortingAndPagingCallbacks.

如果EnableSortingAndPagingCallbacks = TRUE:


  • 寻呼作品

  • 分拣作品

  • 绑定列工作

  • 模板列做的工作

如果EnableSortingAndPagingCallbacks =假:


  • 寻呼作品

  • 排序确实工作

  • 绑定列工作

  • 使用TemplateFields工作


我如何去获得分页,排序,并使用TemplateFields工作,都在同一时间?

How do I go about getting Paging, Sorting, and TemplateFields to work, all at the same time?


的实施澄清:

使用具有一个GridView一个ObjectDataSource需要实现一个称为选择方法,该方法提供了一种前pression,要返回的行数,并开始行

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;
    }

具体SORTEX pression在ASPX / ASCX定义的:

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>

这是应该的中传递,并呼吁ObjectDataSource的Select方法被点击的列时发生,但它似乎没有工作,如果EnableSortingAndPagingCallbacks =真实的,而不是我得到异常没有被定义的排序的事件。

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.

推荐答案

与数据字段的值更改的SORTEX pression值。
设置AllowPaging和AllowSorting为true。
设置EnableSortingAndPagingCallbacks为true。

Change the SortExpression value with the value of DataField. Set AllowPaging and AllowSorting to true. Set EnableSortingAndPagingCallbacks to true.

这篇关于如何使用ObjectDataSource控件与模板列进行排序的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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