Sorting in ListView

Source: Internet
Author: User

The situation described here is:

(1) Use table to layout the ListView.

(2) the data source of ListView is List <T>.

(3) Two sorting fields (the number of replies to the post and the number of views) are of the int type.

 

Basic Ideas:

ListView triggers data source sorting and rebinds the data source to ListView using the Sort () method of the data source (that is, List <T>.

 

Steps:

(1) it can be found that the List <T> Sort () method carries an ICompare <T> generic interface type parameter. Therefore, first construct the types that inherit the generic interface:

[Csharp]
/// <Summary>
/// Reply to the ascending comparison class
/// </Summary>
Public class PostReplyCountAscCompare: IComparer <PostInfo>
{
# Region IComparer <PostInfo> Member
 
Public int Compare (PostInfo x, PostInfo y)
{
Return x. ReplyCount. CompareTo (y. ReplyCount );
}
 
# Endregion
}
/// <Summary>
/// Reply to the descending comparison class
/// </Summary>
Public class PostReplyCountDescCompare: IComparer <PostInfo>
{
# Region IComparer <PostInfo> Member
 
Public int Compare (PostInfo x, PostInfo y)
{
Return y. ReplyCount. CompareTo (x. ReplyCount );
}
 
# Endregion
}
 
 
/// <Summary>
/// View the comparison class in ascending order
/// </Summary>
Public class PostViewCountAscCompare: IComparer <PostInfo>
{
# Region IComparer <PostInfo> Member
 
Public int Compare (PostInfo x, PostInfo y)
{
Return x. ViewCount. CompareTo (y. ViewCount );
}
 
# Endregion
}
/// <Summary>
/// View the comparison class in descending order
/// </Summary>
Public class PostViewCountDescCompare: IComparer <PostInfo>
{
# Region IComparer <PostInfo> Member
 
Public int Compare (PostInfo x, PostInfo y)
{
Return y. ViewCount. CompareTo (x. ViewCount );
}
 
# Endregion
}
Note: The PostInfo model class mentioned above can be fabricated by the reader, but the ViewCount and ReplyCount attributes (int type) are required ).


(2) because there are four sorting rules, they correspond to the four classes in (1. So construct a sort helper class: SortHelper, the Code is as follows:

[Csharp]
Public class SortHelper
{
/// <Summary>
/// Sort the set-generic Method
/// </Summary>
/// <Typeparam name = "T1"> object type in the set </typeparam>
/// <Typeparam name = "T2"> sorting type </typeparam>
/// <Param name = "collection"> set to be sorted </param>
/// <Param name = "comparer"> sequencer </param>
Public static void Sort <T1, T2> (List <T1> collection, T2 comparer) where T2: IComparer <T1>
{
Collection. Sort (comparer );
}
}

(3) design ListView to construct sorting Fields

[Html]
<LayoutTemplate>
<Table class = "PostList">
<Tr class = "PostListHeader">
<Td colspan = "2">
Title
</Td>
<Td>
Release Date
</Td>
<Td>
<Asp: LinkButton runat = "server" ID = "lbtnReply" Text = "reply" CommandName = "Sort" CommandArgument = "ReplyCount"
CssClass = "sortLink"> </asp: LinkButton>
</Td>
<Td>
<Asp: LinkButton runat = "server" ID = "lbtnView" Text = "Browse" CommandName = "Sort" CommandArgument = "ViewCount"
CssClass = "sortLink"> </asp: LinkButton>
</Td>
<Td>
Last Post
</Td>
<Td>
Delete
</Td>
</Tr>
<Tr runat = "server" id = "itemPlaceholder">
</Tr>
</Table>
<Div class = "pager">
<Asp: DataPager ID = "pagerBottom" runat = "server" PageSize = "5">
<Fields>
<Asp: NextPreviousPagerField ButtonCssClass = "command" FirstPageText = "<" PreviousPageText = "<"
RenderDisabledButtonsAsLabels = "true" ShowFirstPageButton = "true" ShowLastPageButton = "false"
ShowNextPageButton = "false" ShowPreviousPageButton = "true"/>
<Asp: NumericPagerField ButtonCount = "7" CurrentPageLabelCssClass = "current" nextpreviusbuttoncssclass = "command"
NumericButtonCssClass = "command"/>
<Asp: NextPreviousPagerField ButtonCssClass = "command" LastPageText = ">" NextPageText = ">"
RenderDisabledButtonsAsLabels = "true" ShowFirstPageButton = "false" ShowLastPageButton = "true"
ShowNextPageButton = "true" ShowPreviousPageButton = "false"/>
</Fields>
</Asp: DataPager>
</Div>
</LayoutTemplate>
Note: The two linkbuttons in the preceding LayoutTemplate are used as user sorting interfaces. The CommandName attribute is Sort (fixed), and CommandArgument is ReplyCount and ViewCount, respectively.


(4) ListView exposes two Sorting-related events: Sorting and Sorted.

[Csharp] view plaincopy
/// <Summary>
/// Sorting by listview
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void lvPosts_Sorting (object sender, ListViewSortEventArgs e)
{
// Determine whether a sorting field is specified
If (string. IsNullOrEmpty (e. SortExpression ))
{
Return;
}
// Data Source
If (ViewState ["posts"]! = Null)
{
Posts = ViewState ["posts"] as List <PostInfo>;
}
Else
{
Posts = new PostInfoBLL (). GetAllPosts (begin, end );
ViewState ["posts"] = posts;
}
// Ascending or descending
If (ViewState ["SortDirection"]! = Null)
{
E. SortDirection = (SortDirection) ViewState ["SortDirection"];
}
 
// Sort by which field
If (e. SortExpression = "ReplyCount ")
{
If (e. SortDirection = SortDirection. Ascending)
{
// Generic method call
SortHelper. Sort <PostInfo, PostReplyCountAscCompare> (posts, new PostReplyCountAscCompare ());
ViewState ["SortDirection"] = SortDirection. Descending;
}
Else
{
SortHelper. Sort <PostInfo, PostReplyCountDescCompare> (posts, new PostReplyCountDescCompare ());
ViewState ["SortDirection"] = SortDirection. Ascending;
}
}
Else if (e. SortExpression = "ViewCount ")
{
If (e. SortDirection = SortDirection. Ascending)
{
SortHelper. Sort <PostInfo, PostViewCountAscCompare> (posts, new PostViewCountAscCompare ());
ViewState ["SortDirection"] = SortDirection. Descending;
}
Else
{
SortHelper. Sort <PostInfo, PostViewCountDescCompare> (posts, new PostViewCountDescCompare ());
ViewState ["SortDirection"] = SortDirection. Ascending;
}
}
BindPosts (true );
}

Note: The data source acquisition and BindPosts () methods in the preceding method can be fabricated by the reader.

(5) run the interface, for example:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.