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 6640441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:21+00:00 2026-05-25T23:40:21+00:00

Sort listview using column headings in the LayoutTemplate I am able to sort a

  • 0

Sort listview using column headings in the LayoutTemplate

I am able to sort a basic list view using asp:SqlDataSource and setting the list view property DataSourceID by pointing it to the asp:SqlDataSource ID. I am having an issue when sorting when not using the asp:SqlDataSource and just DataBinding from the code behind.

SqlDataSource Example:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

<asp:SqlDataSource ID="ContactsDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MainConnString %>"
    SelectCommand="SELECT * FROM TableName">
</asp:SqlDataSource>

DataBind Example:

<asp:ListView ID="ContactsListView" DataSourceID="ContactsDataSource" runat="server">
    <LayoutTemplate>
        <table width="640px" runat="server">
            <tr class="header" align="center" runat="server">
                <td>
                    <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
    </LayoutTemplate>
    ....
</asp:ListView>

protected void Page_Load(object sender, EventArgs e)
{
    String SQL = "SELECT * FROM Customer";
    SqlDataAdapter da= new SqlDataAdapter(SQL, ConnStr);
    DataSet ds = new DataSet();
    da.Fill(ds);

    ContactsListView.DataSource = ds.Tables[0];
    ContactsListView.DataBind();
}

Both code samples populate the list view, but the second example data binding does not work for sorting. With the first example, the sorting just works with the added asp:LinkButton in the LayoutTemplate adding the CommandName=”sort” and setting the CommandArugment=”ColumnName”, but it does not work with the second example.

Can anyone please explain why and how to get the sorting working using the code behind DataBind method?

Thanks!

  • 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-25T23:40:22+00:00Added an answer on May 25, 2026 at 11:40 pm

    I solved my issue.

    I added an event to handle the sorting. The event grabs the command name (Data column) and passes it and the sorting direction into a function which will make another call to the database sort the returned results and rebind to the List View.

    I had to create a view state to hold the List View Sort Direction because for some reason, the onsorting event handler kept saying that the sort direction was ascending.

    Front End

    <asp:ListView ID="ContactsListView" OnSorting="ContactsListView_Sorting" runat="server">
        <LayoutTemplate>
            <table width="640px" runat="server">
                <tr class="header" align="center" runat="server">
                    <td>
                        <asp:LinkButton runat="server" ID="SortByFirstNameButton" CommandName="Sort" Text="First Name" CommandArgument="FirstName" />
        </LayoutTemplate>
        ....
    </asp:ListView>
    

    Back End

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindContacts(string.Empty);
        }
    }
    
    protected SortDirection ListViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    
    protected void ContactsListView_Sorting(Object sender, ListViewSortEventArgs e)
    {
        BindContacts(e.SortExpression + " " + ListViewSortDirection.ToString());
    
        // Check the sort direction to set the image URL accordingly.
        string imgUrl;
        if (ListViewSortDirection == SortDirection.Ascending)
            ListViewSortDirection = SortDirection.Descending;
        else
            ListViewSortDirection = SortDirection.Ascending;
    }
    
    private void BindContacts(string sortExpression)
    {
        sortExpression = sortExpression.Replace("Ascending", "ASC");
        sortExpression = sortExpression.Replace("Descending", "DESC");
        using (SqlConnection conn = new SqlConnection(_connStr))
        {
            conn.Open();
            using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM Customer", conn))
            {
                DataTable dTable = new DataTable();
                dAd.Fill(dTable);
                // Sort now
                dTable.DefaultView.Sort = sortExpression;
                // Bind data now
                ContactsListView.DataSource = dTable;
                ContactsListView.DataBind();
            }
            conn.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort the list items, I'm using custom listview. In this i
I'm using this to sort a list view: http://support.microsoft.com/kb/319401 It's working great, except when
If I am using the ASP.net MVC standard list view is there an easy
Is it possible to define a different multi-column approach for a list view in
I'm attempting to sort a ListView using C#, but whenever I click the sort
I want to sort the items inside column of ListView, i already made it,
I have a button that should sort a ListView using a custom adapter. The
I've a list view and i created header (By using addHeader(layout)) to it, and
To sort a List on multiple criteria, I'm currently doing something like: collection.Sort((f1, f2)
I would like to sort an array in ascending order using C/C++ . The

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.