Please Download the Database from this link
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting"
AutoGenerateColumns="False" CellPadding="5">
<Columns>
<asp:BoundField DataField="ProductID" SortExpression="ProductID" HeaderText="Product ID"
HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="ProductName" SortExpression="productname" HeaderText="Product"
HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="UnitPrice" SortExpression="unitprice" HeaderText="Price"
HeaderStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
C#:
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set { ViewState["sortOrder"] = value; }
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
ViewState["sortOrder"] = "";
bindGridView("", "");
}
}
public void bindGridView(string sortExp, string sortDir)
{
string constr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select * from Products", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
DataView myDataView = new DataView();
myDataView = ds.Tables[0].DefaultView;
if (!string.IsNullOrEmpty(sortExp))
{
myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
}
GridView1.DataSource = myDataView;
GridView1.DataBind();
}
}
}
}
protected void GridView1_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
bindGridView(e.SortExpression, sortOrder);
}
}
Please try this first.
Still if you got the PostBack then Use AsyncPostBackTrigger.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting"
AutoGenerateColumns="False" CellPadding="5">
<Columns>
<asp:BoundField DataField="ProductID" SortExpression="ProductID" HeaderText="Product ID"
HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="ProductName" SortExpression="productname" HeaderText="Product"
HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="UnitPrice" SortExpression="unitprice" HeaderText="Price"
HeaderStyle-HorizontalAlign="Left" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Or you can do this from code behind also in page load.
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(this.GridView1);
Thank You.