Sitefinity ASP.NET CMS - Content Management System

KB Article

Home >  Support >  Knowledge Base >  KB Article
Sorting by column name in Download List control - ID#1045
Rating: Not rated
Last Modified: 7/11/2008
Related categories: Controls;

Article information

Article relates to

 Sitefinity 3.x

Created by

 Penka Ivanova

Last modified by  Rebecca


Problem
I cannot sort documents by title in the Download List control.

Solution
The Download List control does not support sorting on column click out of the box. To achieve this, you need to create your own control that extends the base one. 

Here is what you need to do:

1. Create a copy of the original template (~/Sitefinity/ControlTemplates/Libraries/DownloadListTableModeTemplate.ascx) and modify the copy. You should replace the Literal controls in the repeater HeaderTemplate with LinkButton controls:
   
<asp:Repeater ID="repeater" runat="server"
    <HeaderTemplate> 
        <table class="sf_libraryGrid"
            <thead> 
            <tr> 
                <th scope="col" id="TitleWrap" runat="server"><asp:LinkButton ID="btnName" runat="server" Text="<%$Resources:Title %>" CommandName="Name"></asp:LinkButton></th

2. Create a class that derives from the DownloadList base class:
  • Set TableModeTemplatePath property to point to the new template in the CreateChildControls method   
  • Implement a custom sorting in the repeater:
protected override void SetItemHeaderContent(Control itemContainer, IContent contentItem) 
        { 
            base.SetItemHeaderContent(itemContainer, contentItem); 
            LinkButton btn = itemContainer.FindControl("btnName"as LinkButton; 
            if (btn != null
                btn.Command += new CommandEventHandler(btn_Command); 
        } 
 
        void btn_Command(object sender, CommandEventArgs e) 
        { 
            this.SortData(e.CommandName); 
        } 
        
        void SortData(string sortExpression) 
        { 
            if (ViewState["SortOrder"] == null
            { 
                ViewState["SortOrder"] = " ASC"
            } 
            else if (ViewState["SortOrder"].ToString() == " ASC"
            { 
                ViewState["SortOrder"] = " DESC"
            } 
            else 
            { 
                ViewState["SortOrder"] = " ASC"
            } 
 
            this.SortExpression = sortExpression + ViewState["SortOrder"]; 
 
            this.ListContainer.RepeaterControl.DataBind(); 
        } 





Article Comments

There are no comments yet.
Please Sign In to rate this article or to add it to your favorites.