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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:25:00+00:00 2026-05-27T01:25:00+00:00

i want to do sorting in the listview from code behind, and i have

  • 0

i want to do sorting in the listview from code behind, and i have done it by below code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindLV("");
}

public DataTable GetEmployee(string query)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    SqlDataAdapter ada = new SqlDataAdapter(query, con);
    DataTable dtEmp = new DataTable();
    ada.Fill(dtEmp);
    return dtEmp;
}

private void BindLV(string SortExpression)
{
    string UpdateQuery = "Select * from Employee" + SortExpression;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

    lvEmployee.DataSource = GetEmployee(UpdateQuery);
    lvEmployee.DataBind();
}

protected void lvEmployee_Sorting(object sender, ListViewSortEventArgs e)
{
    ImageButton imEmpID = lvEmployee.FindControl("imEmpID") as ImageButton;
    ImageButton imEmpName = lvEmployee.FindControl("imEmpName") as ImageButton;

    string DefaultSortIMG = "~/img/asc.png";
    string imgUrl = "~/img/desc.png";

    if (ViewState["SortExpression"] != null)
    {
        if (ViewState["SortExpression"].ToString() == e.SortExpression)
        {
            ViewState["SortExpression"] = null;
            imgUrl = DefaultSortIMG;
        }
        else
        {
            ViewState["SortExpression"] = e.SortExpression;
        }
    }
    else
    {
        ViewState["SortExpression"] = e.SortExpression;
    }

    switch (e.SortExpression)
    {
        case "EmpID":
            if (imEmpName != null)
                imEmpName.ImageUrl = DefaultSortIMG;

            if (imEmpID != null)
                imEmpID.ImageUrl = imgUrl;

            break;

        case "EmpName":

            if (imEmpID != null)
                imEmpID.ImageUrl = DefaultSortIMG;

            if (imEmpName != null)
                imEmpName.ImageUrl = imgUrl;

            break;
    }
    BindLV(" order by " + e.SortExpression + " " + ((ViewState["SortExpression"] != null) ? "ASC" : "DESC"));
}

but the problem is i’m using another function for data paging as below in code behind which is contain of sorting as well:

 protected void DataPager1_PreRender(object sender, EventArgs e)
{
    lvEmployee.DataSource = GetEmployee("Select * from Employee");
    lvEmployee.DataBind();

}

and my data pager code which is located at LayoutTemplate in .aspx page:

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvEmployee" PageSize="5" onprerender="DataPager1_PreRender">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="true"/>
                </Fields>
            </asp:DataPager>

Every time i click at the name to sort it, there will be no change in the list. E have traced the problem, and I have found out that the sorting function is working properly. But before the page come up, the DataPager1_PreRender function is called and again shows the list without sorting.

Could you please guide me how to do sorting and dataPaging together without this problem. Appreciate your consideration.

  • 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-27T01:25:01+00:00Added an answer on May 27, 2026 at 1:25 am

    I have over come to this problem by using Session.
    before i use session i found out every time i press the next page or anytime the page refresh it will execute the DataPager1_PreRender() and i have set the data source to

    GetEmployee("Select * from Employee");
    

    that’s why the sorting never happens.
    i have add Session["UpdateQT"] = UpdateQuery; to BindLV() to keep the update query and i have change the DataPager1_PreRender() to

    string strtmp = Session["UpdateQT"].ToString();
        if (strtmp == null) 
        {
            strtmp = "Select * from Employee";
        }
    
        lvEmployee.DataSource = GetEmployee(strtmp);
        lvEmployee.DataBind();
    

    to keep the latest query after sorting.
    i hope it helps somebody.

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

Sidebar

Related Questions

i want to do nested sorting . I have a course object which has
I have a huge table and I want simple sorting. It could be so
I want to have a model with calculated fields that I can apply sorting
I want to do custom sorting on a ListView which has a DataTable as
I want to sort a ListView control in C# based on columns. I have
Hey all, I have a winforms virtualized Listview that i want to be able
I want the default sorting to be rating DESC, but I also have, some
i want to do sorting in the mvc contrib grid....so far i have i
I have a ListView (GridView) that I want to sort by 2 columns, so
I have a table and i want to do sorting function for each column.

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.