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

问题描述 投票:7回答:3

背景:

我正在使用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工作

我的问题:

如何让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;
    }

特定的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则它似乎不起作用,而是我得到关于未定义Sorting事件的异常。

asp.net gridview objectdatasource templatefield
3个回答
0
投票

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


0
投票

用于排序功能:

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

0
投票

使用DataField的值更改SortExpression值。将AllowPaging和AllowSorting设置为true。将EnableSortingAndPagingCallbacks设置为true。

© www.soinside.com 2019 - 2024. All rights reserved.