1

私は少し奇妙な問題を抱えています。以下の方法を使用して、リストビューヘッダーをクリックして ASC/DESC 順にソートする ListView があります。ObjectDataSource を定義してそれを ListView にアタッチすると、完全に機能します。

を使用して手動バインディングを使用するだけの場合

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

ソートが機能しなくなります。sort メソッドで再バインディングを試みましたが、それでも機能しません。何か不足していますか?

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 に答える