I am having a bit of a strange issue.
I have a ListView which I sort in ASC/DESC order by clicking the listview header, using the method below, now the thing is that works perfectly when I have an ObjectDataSource defined and attach that to the ListView.
Now if I just use manual binding using
listview.DataSource = GetListViewContent();
listview.DataBind();
the sorting no longer works. I have tried re-binding in the sort method but still it does not work. Am I missing something?
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() is the source for manual and auto sources and for display purposes both work to show the data; but sorting only works in the auto.
You also need to rebind sorted data. So maybe you should consider implementig your method this way:
GetListViewContent(SortDirection sortDir);