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

  • SEARCH
  • Home
  • 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 869875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:22:46+00:00 2026-05-15T10:22:46+00:00

I have an asp.net ListView that is sortable. I have a button with a

  • 0

I have an asp.net ListView that is sortable.

I have a button with a “select” command name. When I click on the button the appropriate row gets selected. If I then click on a sort header the ListView will sort, but the selected index will stay the same. In other words if I click the 2nd row then sort the 2nd row is still selected.

Is there a way to make the ListView select the appropriate row after it sorts so that if I click an item then sort the same item will still be selected but in a different position depending on the sort?

  • 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-05-15T10:22:47+00:00Added an answer on May 15, 2026 at 10:22 am

    You have to do it programmatically – although the solution is somewhat nasty.
    First step is to define DataKeys and onSorting and Sorted events in ListView as below

      <asp:ListView ID="ListView1" runat="server"  DataSourceID="SqlDataSource1"  DataKeyNames="AddressId,AddressLine1"
                onsorting="ListView1_Sorting" onsorted="ListView1_Sorted">
    

    Then in the code behind you have to handle the events.Since the DataItems on the Items collection is always null and DataIndex and DisplayIndex are not set as one would normally expect we have to use DataKeys.Store datakey of selected Item before sort and after sort search through DatakEy collection to match with stored datakey. See below

     private DataKey dk;
    
            protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
            {
              dk=  (ListView1.SelectedIndex > 0) ? ListView1.DataKeys[ListView1.SelectedIndex] : null;
            }
            protected void ListView1_Sorted(object sender, EventArgs e)
            {
                if (dk == null) return;
                int i;
                ListView1.DataBind();
                for (i = 0; i < ListView1.DataKeys.Count; i++)
                   if(AreEqual(ListView1.DataKeys[i].Values,dk.Values)) break;
                if (i >= ListView1.DataKeys.Count) return;
                ListView1.SelectedIndex =i;
            }
            private bool AreEqual(System.Collections.Specialized.IOrderedDictionary x, System.Collections.Specialized.IOrderedDictionary y)
            {
                for (int i = 0; i < x.Count; i++)
                    if (!x[i].Equals(y[i])) return false;
                return true;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET page with an asp:button that is not visible. I can't
I have an asp.net listview. The data that populates it is sorted into groups,
I have asp.net form that contains fields. When I access this window, my javascript
I have a ASP.NET application that we've written our own logging module for. My
I have an ASP.NET ListView control (see below). Unfortunately, when a ListView control is
I have a paged ASP.NET ListView. The data shown is filtered, which can be
I have an ASP.NET 2.0 ListView control (aka:parent) and configured inside this ListView I
I'm using the new ASP.Net ListView control to list database items that will be
I am using an ASP.NET ListView control and, at the moment, I have a
I have ASP.NET web pages for which I want to build automated tests (using

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.