Asp.net 如何使用ObjectDataSource和TemplateFields在GridView上排序 背景:

Asp.net 如何使用ObjectDataSource和TemplateFields在GridView上排序 背景:,asp.net,gridview,objectdatasource,templatefield,Asp.net,Gridview,Objectdatasource,Templatefield,我正在使用GridView和ObjectDataSource。我正在实现分页和排序 在ObjectDataSource上: objectDataSource.TypeName = value; objectDataSource.SelectMethod = "Select"; objectDataSource.SelectCountMethod = "SelectCount"; objectDataSource.SortParame

我正在使用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上的回调。”

这是有道理的。我只需要知道如何使排序工作,而不使用启用排序和分页回调

如果启用排序和分页回调=True:

  • 传呼工作
  • 分拣工作
  • 边界工作
  • 模板字段不起作用
如果启用排序和分页回调=False:

  • 传呼工作
  • 排序不起作用
  • 边界工作
  • 模板字段工作

我的问题: 如何让分页、排序和模板字段同时工作


关于实施的澄清:

将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;
    }
public IEnumerable Select(字符串排序表达式、int-maximumRows、int-startRowIndex)
{
string oql=“通过{”+sortExpression+“}选择县顺序”;
var countries=QueryProvider.ExecuteQuery(oql.Cast();
var page=countries.Skip(startRowIndex).Take(maximumRows);
变量行=页面。选择(
county=>新CountyAndStateGridRow
{
CountyName=county.Name,
StateName=country.State.Name,
});
返回行;
}
特定的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,则它似乎不起作用,相反,我得到了一个关于未定义排序事件的异常。

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

要使排序功能正常工作,请执行以下操作:

 <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;
    }
protectedvoid GvCountryDetails\u页面索引交换(对象发送方,GridViewPageEventArgs e)
{
GvCountryDetails.PageIndex=e.NewPageIndex;
isPageIndexChanged=true;
BindData();
}
受保护的void GvCountryDetails\u排序(对象发送器、GridViewSortEventArgs e)
{
sortExpression=e.sortExpression;
isPageIndexChanged=false;
BindData();
}
私有void sortgridata()
{
串式TDIR;
if(isPageIndexChanged==true)
{
sSortdir=ViewState[“SortDirection”]作为字符串;
}
其他的
{
sSortdir=GetSortDirection(sortExpression);
}
字符串sSortExp=sortExpression;
如果(sSortdir==“ASC”)
{
lstCountryDetails=排序(lstCountryDetails、ssortextp、SortDirection.升序);
}
其他的
{
lstCountryDetails=排序(lstCountryDetails、ssortextp、SortDirection.Descending);
}
}
私有列表排序(列表、字符串排序、排序方向)
{
PropertyInfo property=list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
if(方向==排序方向.升序)
{
return list.OrderBy(e=>property.GetValue(e,null)).ToList();
}
其他的
{
return list.OrderByDescending(e=>property.GetValue(e,null)).ToList();
}
}
私有字符串GetSortDirection(字符串列)
{
字符串sortDirection=“ASC”;
字符串sortExpression=ViewState[“sortExpression”]作为字符串;
if(sortExpression!=null)
{
if(sortExpression==列)
{
字符串lastDirection=ViewState[“SortDirection”]作为字符串;
如果((lastDirection!=null)&&(lastDirection==ASC”))
{
sortDirection=“DESC”;
}
}
}
ViewState[“SortDirection”]=SortDirection;
ViewState[“SortExpression”]=列;
返回排序方向;
}

使用数据字段的值更改SortExpression值。 将AllowPaging和AllowSorting设置为true。
将EnableSortingAndPagingCallbacks设置为true。

当EnableSortingAndPagingCallbacks设置为true时,页面会调用服务器。(即,它不是纯粹的客户端。)当我单击一个可排序列时,ObjectDataSource指定的数据源上的Select方法实际上是以列名作为sortExpression参数调用的,然后将其转换为代码中带有“order by”的查询。