sort data within a repeater

Farah El Agha asked on November 10, 2017 13:45

hello, so I have a repeater that contains data, and shown as a form of a table, what I want is when I click on the header (lets say the header of Dates or Names) the data will be sorted ASC and DESC. Is it possible to do so?

Correct Answer

Brenden Kehren answered on November 10, 2017 15:58

Look at the HTML closely.

<a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%# "FileDate+" + GetSortDirection() %>">Date </a>

Notice how it's encoded using "+"? The above code will render the following HTML:

<a href="/About-Us/Meeting-Minutes?searchtext=&amp;searchmode=&amp;sort=FileDate+asc">Date</a>

So the URL will not have a space and the GetSortDirection() method takes that into consideration.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Jan Hermann answered on November 10, 2017 14:17 (last edited on December 10, 2019 02:31)

Yes, you can link the very same page with some query string like ?sort=field DESC and then you can use this value as the Order by expression of your repeater with a help of macros:

{% QueryString.GetValue("sort") |(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 10, 2017 15:06

This is an example of what we use. Very similar to what Jan is explaining but with a few more tweaks.

In the transformation we use this method to get the sort direction to append to the header link:

<script runat="server">
    public string GetSortDirection()
    {
        string sort = ValidationHelper.GetString(HttpUtility.UrlDecode(Request.QueryString["sort"]), "");
        string[] sortString = sort.Split(' ');
        string sortDir = "asc";
        if (sortString.Length >= 2)
        {
            sortDir = sortString[1];
            if(sortDir.ToLower().Equals("asc"))
            {
                sortDir = "desc";
            }
            else
            {
                sortDir = "asc";
            }
        }
        return sortDir;
    }
</script>

Then in the actual transformation we add a placeholder so we can dynamically generate the opening table tag and the header row.

<asp:PlaceHolder ID="ph1" runat="server" Visible='<%# IsFirst() %>'>
<div class="unigrid-content">
  <table class="table">
    <thead>
      <tr class="unigrid-head">
         <th><a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%#  "FileName+" + GetSortDirection() %>">Name </a></th>
         <th><a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%#  "FileDate+" + GetSortDirection() %>">Date </a></th>
                 <th><a href="<%# CurrentDocument.NodeAliasPath %>?searchtext=<%# Request.QueryString["searchtext"] %>&searchmode=<%# Request.QueryString["searchmode"] %>&sort=<%#  "Attachement_1+" + GetSortDirection() %>">Attachment</a></th>       
      </tr>
    </thead>
    <tbody>
</asp:PlaceHolder>

Now we have the standard item text (or table row in our case):

<tr>
    <td><a href='<%# GetAbsoluteUrl("/getattachment/" + Eval("FileAttachment")) %>' title="Download" target="_blank"> <%# GetSearchValue("FileName") %></a></td>
    <td><%# FormatDateTime(Eval("FileDate"), "MM/dd/yyyy") %></td>
     <td><a href="<%# GetAbsoluteUrl("/getattachment/" + Eval("Attachment_1")) %>">
    &#9660;<%# Eval("Attachment1Name") %>
  </a></td>
</tr>   

Lastly, we close out the opening div and table tags using a similar approach with the placeholder.

<asp:PlaceHolder ID="ph2" runat="server" Visible='<%# IsLast() %>'>
      </tbody>
    </table>
  </div>
</asp:PlaceHolder>

Now for the configuration of the Search sort property in the webpart. We use this macro to get the "sort" querystring parameter.

`{% if(QueryString.GetValue("sort") == ""){"FileName ASC"}else{UrlDecode(QueryString.GetValue("sort"))} @%}`

Of course you will have to make modifications to your field names but for the most part this solution should work just fine.

1 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on November 10, 2017 15:11

Hi Brenden,

you can make the last part a bit cleaner using the default value:

{% UrlDecode(QueryString.GetValue("sort","FileName ASC")) @%}

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 10, 2017 15:22

Good call Jan! It's been a while since I last used that code so haven't worked through it yet to clean it up.

0 votesVote for this answer Mark as a Correct answer

hassan kalach answered on November 10, 2017 15:49

and how would I add the ?sort=field DESC string to URL? also it contains a space between field and DESC which is not acceptable in URLs. what should be the script to use in such case?

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on November 10, 2017 20:41

I don't see any problem using spaces within query string values though.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.