1

我有一个奇怪的问题。我有一个 ListView,我通过单击 listview 标题,使用下面的方法按 ASC/DESC 顺序排序,现在当我定义了 ObjectDataSource 并将其附加到 ListView 时,它可以完美地工作。

现在,如果我只使用手动绑定

listview.DataSource = GetListViewContent();
listview.DataBind();

排序不再起作用。我已经尝试在排序方法中重新绑定,但它仍然不起作用。我错过了什么吗?

protected void lvFullReport_Sorting(object sender, ListViewSortEventArgs e)
    {
        Control me = (Control)sender,
           headerRow = me.FindControl("headerRow");

         //Assume that the "header row" control's "control collection" just contains "th"-like control,
         //whose type is exactly "HtmlTableCell" . While we just utilize its properties in the "HtmlControl" level
         //so we cast them as "HtmlControl".
         //What's more , as for these "th" controls , just those who contains an "IButtonControl" ( sorting triggers)
         //are really needed.


        foreach (System.Web.UI.HtmlControls.HtmlControl sortCell in headerRow.Controls.Cast<System.Web.UI.HtmlControls.HtmlControl>()
            .Where(th => th.Controls.OfType<IButtonControl>().Any()))
        {
              //Get out the "only" sorting-Button Control ,
              //for that in a "th" those empty space or literal text area are treated as "Literal Control" ,
              //"literal" fills whole space of "th".

            IButtonControl btnSortField = sortCell.Controls.OfType<IButtonControl>().Single();

            if (btnSortField.CommandArgument == e.SortExpression)
                sortCell.Attributes["class"] = e.SortDirection == SortDirection.Ascending ? "up" : "down";
            else
                if (sortCell.Attributes["class"] != null) sortCell.Attributes.Remove("class");
        }

        DisplayChart();
    }

GetListViewContent() 是手动和自动来源的来源,出于显示目的,两者都可以显示数据;但排序只适用于汽车。

4

2 回答 2

0

您还需要重新绑定已排序的数据。所以也许你应该考虑这样实现你的方法:

GetListViewContent(SortDirection sortDir);

于 2012-06-18T08:23:18.090 回答
0

使用对象数据源时,我也总是得到 e.SortDirection = SortDirection.Ascending。我发现以下有用的帖子: http:
//www.codedigest.com/articles/aspnet/412_sorting_in_aspnet_listview_control_-_binding_in_codebehind.aspx

这导致我采用以下解决方案将 SortExpression 存储在 ViewState 中,然后用正确的值覆盖 e.SortDirection。

protected void ListView_Sorting(object sender, ListViewSortEventArgs e)
{
    // Override sort direction (since it's always ascending when 
    // we're using an object data source)
    e.SortDirection = GetListViewSortDirection(e.SortExpression);

    // Rebind data
    ProductListView.DataSource = 
        GetListViewContent(e.SortExpression, e.SortDirection);
    ProductListView.DataBind();
}

private SortDirection GetListViewSortDirection(string sortExpression)
{
    // Store sort expression in viewstate
    SortDirection listViewSortDirection = SortDirection.Ascending;
    if (ViewState["SortExpression"] != null 
        && ViewState["SortExpression"].ToString() == sortExpression)
    {
        ViewState["SortExpression"] = null;
        listViewSortDirection = SortDirection.Descending;
    }
    else
    {
        ViewState["SortExpression"] = sortExpression;
    }

    return listViewSortDirection;
}
于 2012-08-15T20:59:26.560 回答