Here is the problem:
When you click LinkButton, the first thing that happens is a page reload. However, Response.Redirect(Request.RawUrl) does not save the ViewState, so the sort order is lost. Therefore, GridView is populated with unsorted data.
Then the LinkButton onClick event function is called. The object passed to this is a LinkButton from the correct row number, but since the sort order of the table has been changed (back to its unsorted state), the LinkButton in this row was no longer the LinkButton that the user clicked. Therefore, the command argument was incorrect.
To fix the problem:
I changed all ViewState [string] to Session [string] (so that the sort direction is preserved when the page reloads) and added the following code to the Page_Load function before binding the GridView:
if (Session["companiesExpression"] != null && Session["companiesDirection"] != null) { companies.DefaultView.Sort = Session["companiesExpression"] + " " + Session["companiesDirection"]; }
source share