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