C# asp.net listview排序和手动数据绑定
我有点奇怪的问题。 我有一个ListView,我通过单击ListView标题,使用下面的方法按ASC/DESC顺序排序,现在的问题是,当我定义了ObjectDataSource并将其附加到ListView时,它可以完美地工作 现在,如果我只是使用手动绑定C# asp.net listview排序和手动数据绑定,c#,asp.net,listview,sorting,data-binding,C#,Asp.net,Listview,Sorting,Data Binding,我有点奇怪的问题。 我有一个ListView,我通过单击ListView标题,使用下面的方法按ASC/DESC顺序排序,现在的问题是,当我定义了ObjectDataSource并将其附加到ListView时,它可以完美地工作 现在,如果我只是使用手动绑定 listview.DataSource = GetListViewContent(); listview.DataBind(); 排序不再有效。我尝试过在sort方法中重新绑定,但仍然不起作用。我错过什么了吗 protected void l
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();
}
受保护的void lvFullReport_排序(对象发送者,ListViewSortEventArgs e)
{
控制我=(控制)发送方,
headerRow=me.FindControl(“headerRow”);
//假设“标题行”控件的“控件集合”只包含类似于“th”的控件,
//它的类型正是“HtmlTableCell”。而我们只是在“HtmlControl”级别使用它的属性
//所以我们将它们转换为“HtmlControl”。
//更重要的是,对于这些“th”控件,只有那些包含“IButtonControl”(排序触发器)的控件
//我们真的很需要。
foreach(headerRow.Controls.Cast()中的System.Web.UI.HtmlControls.HtmlControl sortCell)
.Where(th=>th.Controls.OfType().Any())
{
//拿出“唯一”分拣按钮控件,
//对于“th”中的那些空白或文本区域,将被视为“文本控件”,
//“literal”填充了“th”的整个空间。
IButtonControl bSensortField=sortCell.Controls.OfType().Single();
if(btnSortField.CommandArgument==e.SortExpression)
sortCell.Attributes[“class”]=e.SortDirection==SortDirection.Ascending?“向上”:“向下”;
其他的
如果(sortCell.Attributes[“class”!=null)sortCell.Attributes.Remove(“class”);
}
显示图表();
}
GetListViewContent()是手动源和自动源的源,出于显示目的,两者都用于显示数据;但是排序只在自动模式下起作用。您还需要重新绑定已排序的数据。所以也许你应该考虑用这样的方法来实现你的方法:
GetListViewContent(SortDirection-sortDir)
当使用对象数据源时,我也总是得到e.SortDirection=SortDirection.singressing。我发现了以下有用的帖子:这使我找到以下解决方案,将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;
}
为什么不试试这个@MMLK,你提供的链接中的实时演示不起作用。它返回500服务器错误。感谢链接,但我的代码实际上正在工作,我确信我只是缺少一些非常基本的东西来修复手动绑定。我有点急着要实施另一个解决方案。您能确认您的TemplateFields设置了SortExpression吗?是的,它们设置了SortExpression,因此在不手动绑定源代码时它可以工作。