如何使用 ObjectDataSource 和 TemplateFields 在 GridView 上排序

发布于 2024-07-23 18:33:12 字数 2553 浏览 5 评论 0原文

背景:

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-07-30 18:33:12

为了使排序功能正常工作:

 <asp:GridView GridView ID="GvCountryDetails" AllowPaging="True" 
OnPageIndexChanging="GvCountryDetails_PageIndexChanging" AllowSorting="True" 
onsorting="GvCountryDetails_Sorting">

在 .cs 文件中,您需要编写

protected void GvCountryDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvCountryDetails.PageIndex = e.NewPageIndex;
        isPageIndexChanged = true;
        BindData();
    }

protected void GvCountryDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        sortExpression = e.SortExpression;
        isPageIndexChanged = false;
        BindData();
    }
    private void SortGridData()
    {
        string sSortdir;
        if (isPageIndexChanged == true)
        {
            sSortdir = ViewState["SortDirection"] as string;
        }
        else
        {
            sSortdir = GetSortDirection(sortExpression);
        }

        string sSortExp = sortExpression;

        if (sSortdir == "ASC")
        {
            lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Ascending);
        }
        else
        {
            lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Descending);
        }
    }

    private List<CountryBO> Sort<TKey>(List<CountryBO> list, string sortBy, SortDirection direction)
    {
        PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
        if (direction == SortDirection.Ascending)
        {
            return list.OrderBy(e => property.GetValue(e, null)).ToList<CountryBO>();
        }
        else
        {
            return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Country>();
        }
    }

    private string GetSortDirection(string column)
    {
        string sortDirection = "ASC";
        string sortExpression = ViewState["SortExpression"] as string;
        if (sortExpression != null)
        {
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }

For sorting functionality to work:

 <asp:GridView GridView ID="GvCountryDetails" AllowPaging="True" 
OnPageIndexChanging="GvCountryDetails_PageIndexChanging" AllowSorting="True" 
onsorting="GvCountryDetails_Sorting">

in .cs file you need to write

protected void GvCountryDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvCountryDetails.PageIndex = e.NewPageIndex;
        isPageIndexChanged = true;
        BindData();
    }

protected void GvCountryDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        sortExpression = e.SortExpression;
        isPageIndexChanged = false;
        BindData();
    }
    private void SortGridData()
    {
        string sSortdir;
        if (isPageIndexChanged == true)
        {
            sSortdir = ViewState["SortDirection"] as string;
        }
        else
        {
            sSortdir = GetSortDirection(sortExpression);
        }

        string sSortExp = sortExpression;

        if (sSortdir == "ASC")
        {
            lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Ascending);
        }
        else
        {
            lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Descending);
        }
    }

    private List<CountryBO> Sort<TKey>(List<CountryBO> list, string sortBy, SortDirection direction)
    {
        PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
        if (direction == SortDirection.Ascending)
        {
            return list.OrderBy(e => property.GetValue(e, null)).ToList<CountryBO>();
        }
        else
        {
            return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Country>();
        }
    }

    private string GetSortDirection(string column)
    {
        string sortDirection = "ASC";
        string sortExpression = ViewState["SortExpression"] as string;
        if (sortExpression != null)
        {
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }
尘世孤行 2024-07-30 18:33:12

属性 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.

怪我入戏太深 2024-07-30 18:33:12

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文