Sort ListView not sort SortExpression and SortDirection

I am working on a ListView , and I feel that I am doing something fundamentally wrong when it comes to implementing sorting. Instead of depending on the values โ€‹โ€‹of List1.SortExpression and List1.SortDirection , I resort to hidden fields because List1.SortExpression always empty and List1.SortDirection always SortDirection.Ascending .

On my .aspx page: (irrelevant code edited)

 <asp:HiddenField runat="server" ID="hdnSortExpression" /> <asp:HiddenField runat="server" ID="hdnSortDirection" /> <asp:ListView runat="server" ID="List1" OnItemCommand="List1_ItemCommand" OnSorting="List1_Sorting"> <LayoutTemplate> <table border="0" cellpadding="1"> <thead> <tr> <th><asp:LinkButton runat="server" ID="BtnCompanyCode" CommandName="Sort" CommandArgument="CompanyCode" Text="Company Code" /></th> ... more columns ... </tr> </thead> <tbody> <tr runat="server" id="itemPlaceholder"></tr> </tbody> </table> </LayoutTemplate> 

In my code:

 protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e) { // empty for now } protected void List1_Sorting(object sender, ListViewSortEventArgs e) { SortDirection sortDirection; String sortExpression = e.SortExpression; // how we need to do it in Sorting if (hdnSortExpression.Value.ToLower() == sortExpression.ToLower()) sortDirection = hdnSortDirection.Value == SortDirection.Ascending.ToString() ? SortDirection.Descending : SortDirection.Ascending; else sortDirection = SortDirection.Ascending; DoSortList(sortExpression, sortDirection); // this sets column headings' sort indicator arrows List1_BindData(sortExpression, sortDirection); // sets DataSource and calls DataBind() // the hacky part: setting hidden fields to store sortExpression and sortDirection hdnEligibilitySortExpression.Value = sortExpression; hdnEligibilitySortDirection.Value = sortDirection.ToString(); } private void List1_BindData(String sortExpression, SortDirection sortDirection) { List<SomeEntity> list = null; list = SomeEntity.GetBySsn(_ssn).ToList(); // methods generated by an ORM pointing to an Oracle database switch (sortExpression.ToLower()) { case "CompanyCode": if (sortDirection == SortDirection.Ascending) List1.DataSource = list.OrderBy(o => o.CompanyCode); else List1.DataSource = list.OrderByDescending(o => o.CompanyCode); break; ... other cases ... } List1.DataBind(); } 

This works - each column is sorted correctly by clicking on the same column, it changes the sort direction, but I must conclude that I connected it incorrectly, because I cannot depend on the SortDirection and SortExpression . What am I doing wrong?

+4
source share
2 answers

Take a look at the MSDN example . You will notice that the DataSource is a SqlDataSource. This is one type of DataSource that .Net understands how to sort. Others, not so many.

Here is another MSDN article that indicates data types:

Data source controls that provide native sorting support are LinqDataSource, ObjectDataSource, SqlDataSource, and AccessDataSource controls.

+2
source

Instead of using e.SortDirection, you can pull it from a ListView, such as List1.SortDirection and List1.SortExpression?

0
source

Source: https://habr.com/ru/post/1437632/


All Articles