asp.net listview排序和手动数据绑定

时间:2012-06-18 08:08:16

标签: c# asp.net listview sorting data-binding

我有一个奇怪的问题。 我有一个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()是手动和自动源的来源,出于显示目的,它们都用于显示数据;但排序仅适用于汽车。

2 个答案:

答案 0 :(得分:0)

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

GetListViewContent(SortDirection sortDir);

答案 1 :(得分:0)

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

这使我得到以下解决方案,以便在ViewState中存储SortExpression,然后使用正确的值覆盖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;
}