Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:39:38+00:00 2026-06-12T08:39:38+00:00

I’m working on a ListView and am getting that feeling that I’m doing something

  • 0

I’m working on a ListView and am getting that feeling that I’m doing something fundamentally wrong when it comes to implementing sorting. Rather than being able to depend on the values of List1.SortExpression and List1.SortDirection, I’m resorting to hidden fields because List1.SortExpression is always blank and List1.SortDirection is always SortDirection.Ascending.

On my .aspx page: (edited out irrelevant code)

<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 Behind:

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();
}

It works — each column gets sorted correctly, clicking the same column reverses sort direction, but I have to conclude that I’ve wired things up incorrectly because I cannot depend on the SortDirection and SortExpression properties. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:39:39+00:00Added an answer on June 12, 2026 at 8:39 am

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

    Here’s another MSDN article that specifies the DataSource types:

    The data source controls that provide built-in support for sorting are the LinqDataSource, ObjectDataSource, SqlDataSource, and AccessDataSource controls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.