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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:40:03+00:00 2026-06-13T23:40:03+00:00

I am using a ListView to display paginated data: <asp:ListView ID=listOfItems runat=server DataSourceID=ItemsDataSource EnableModelValidation=True

  • 0

I am using a ListView to display paginated data:

<asp:ListView ID="listOfItems" runat="server" DataSourceID="ItemsDataSource" EnableModelValidation="True" InsertItemPosition="FirstItem" ItemPlaceholderID="ItemRowContainer">
    <LayoutTemplate>
        <div class="tablecontainer">
            <div class="pagination-top">
                <custom:TablePaginationControl ID="TablePaginationControl1" runat="server" ControlID="listOfItems" ShowPageSizeList="true" />
            </div>
            <table class="list-view">
                <tr>
                    <th class="first-column" width="350px">
                        <asp:LinkButton ID="SortByName" runat="server" CommandArgument="Name" CommandName="SortMainList" OnCommand="SortItems" Text="<%$ Resources:Name %>"></asp:LinkButton>
                    </th>
                    ...
                </tr>
                <tbody>
                    <tr runat="server" id="ItemRowContainer" />
                </tbody>
            </table>
        </div>
    </LayoutTemplate>
    ...
</asp:ListView>

The datasource definition:

<asp:ObjectDataSource ID="ItemsDataSource" runat="server" EnablePaging="True" InsertMethod="AddItems" SelectCountMethod="SelectItemsCount" SelectMethod="SelectItems" TypeName="SHLCentral.TheLibrary.Web.View.DocumentManagementControl, ClientPortal.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd2852a10d692fb9" UpdateMethod="UpdateItems">
    ...
</asp:ObjectDataSource>

The revelant code behind is made of these two methods:

public IEnumerable<ListDocumentsResult> SelectItems(
    int maximumRows,
    int startRowIndex)
{
    var results = Controller.ListDocuments(new ListDocumentsRequest());

    PropertyInfo sortProperty;
    try
    {
        sortProperty = typeof (ListDocumentsResult).GetProperty((string) ViewState["mainListSortColumn"]);
    }
    catch
    {
        sortProperty = null;
    }
    Func<ListDocumentsResult, object> sortFunction = sortProperty == null
                            ? (Func<ListDocumentsResult, object>) (ldr => ldr.LastUpdatedDate)
                            : (ldr => sortProperty.GetValue(ldr, new object[0]));

    return
        (sortProperty == null || !((bool) ViewState["mainListSortAsc"])
             ? results.OrderByDescending(sortFunction)
             : results.OrderBy(sortFunction))
            .Skip(startRowIndex)
            .Take(maximumRows);
}

protected void SortItems(object sender, CommandEventArgs e)
{
    if (e.CommandName == "SortMainList")
    {
        var sortColumn = (string) e.CommandArgument;
        if ((string)ViewState["mainListSortColumn"] == sortColumn)
        {
            ViewState["mainListSortAsc"] = !(bool)ViewState["mainListSortAsc"];
        }
        else
        {
            ViewState["mainListSortAsc"] = true;
            ViewState["mainListSortColumn"] = sortColumn;
        }
        DataBind();
    }
}

So my intention this: when the users clicks on the LinkButton contained in the “Name” column header (I left out all but one column for clarity), the SortItems method is called: it sets the sorted column name and sort order into the ViewState, then reloads the ListView using the DataBind method. In the Select method of the ObjectDataSource, we read this ViewState values and use them to order the data.

Putting breakpoints on all these methods, I can see there is this sequence of calls when I click the LinkButton:

  • OnLoad
  • SortItems
  • SelectItems

The problem I have is that when I get to the SelectItems method, the ViewState is totally empty (it has 0 keys): if I set a breakpoint on the Load method of the page, I see the control containing all this is only loaded once. The DataBind method does not seem to trigger any loading of the control, it seems to be just triggering the SelectItems method of a new instance of the control (meaning that if, instead of using ViewState, I set an instance field in the SortItems method, the field is null when getting in the SelectItems method).

I am sure that the ViewState is active on the page (I can find the ViewState keys on the browser side using a Firefox extension for instance).

There is something not quite clear to me about the life cycle of the page/control. Could someone explain what it is to me?

  • 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-13T23:40:04+00:00Added an answer on June 13, 2026 at 11:40 pm

    There exist much, much simpler approach.

    First, instead of a custom CommandName, you put a built-in name into the sort link button. The name is Sort.

    You have then

       <asp:LinkButton ID="SortByName" runat="server" CommandArgument="Name" CommandName="Sort" />
    

    Then, on your ObjectDataSource you add the SortParameterName to be something like OrderBy:

       <ObjectDataSource .... SortParameterName="OrderBy" />
    

    Then you modify your data provider method to be:

     public IEnumerable<ListDocumentsResult> SelectItems(
       string OrderBy,
       int maximumRows,
       int startRowIndex)
    

    The data source will provide the value automatically based on the command argument (Name) and it will automatically append DESC whenever you click the command button for the second time (it is because the ListView persists the state of sort order in its viewstate automatically, you don’t have to reinvent this!)

    Then, you don’t need this ugly delegates to order by strings for linq. Instead, download the Dynamic Linq library:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    find the Dynamic.cs file, include it in your project and it will add a bunch of additional linq operators, including the OrderBy which accepts strings and which automatically supports DESC (!).

    You then just

     public IEnumerable<ListDocumentsResult> SelectItems(
       string OrderBy,
       int maximumRows,
       int startRowIndex)
     {
    
         Controller.ListDocuments(new ListDocumentsRequest())
            .OrderBy(OrderBy)
            .Skip(startRowIndex)
            .Take(maximumRows);
     }
    

    This is just as simple!

    Be warned though that there’s a small bug (or an inconvenience) in the dynamic linq – it throws an exception when the sort order is empty.

    Find then this code (line 47 and down)

        public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (ordering == null) throw new ArgumentNullException("ordering");
    

    and change it manually to

        public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
            if ( string.IsNullOrEmpty( ordering ) ) return source;
            if (source == null) throw new ArgumentNullException("source");
            if (ordering == null) throw new ArgumentNullException("ordering");
    

    Done.

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

Sidebar

Related Questions

I am using listview to display data, inside listview i am using image in
I am using listview to display data, inside listview i am using image in
i'm using jquery listview to display list of course name. the data needs to
I'm using the Listview to display my data. On the ItemDatabound event I want
I am querying the ContactsContract.Data table in Android using a ListView to display the
I am using listview to display three textviews(values for these text views where come
I have a ListView which is using a GridView to display a DataTable and
I am using a listview to display some items.But sometimes based on condition I
I'm using linqdatasource for displaying data in listview (nested because of grouping) control. I
I am using a ListView to display items. Currently I am passing a String

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.