I am performing a search operation in GridView and getting results as expected, but after getting results, if I sort them or click on pagination, the page getting reloaded and sorting and pagination happening for the whole grid. I want sorting and paging should only work for search results which I have got.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- bindgrid();
- }
- }
-
- protected void bindgrid()
- {
- ViewState["sortexp"] = "";
- ViewState["orderby"] = "ASC";
- Sort("");
- int iTotalRecords = ((DataTable)(GridView1.DataSource)).Rows.Count;
- lbldisplay.Text = "Total Products: " + iTotalRecords.ToString();
- }
-
- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GridView1.PageIndex = e.NewPageIndex;
- GridView1.DataBind();
- bindgrid();
- }
- public void Sort(string sortexp)
- {
- DataTable dt = (DataTable)bll.LoadProduct();
- DataView DV = dt.DefaultView;if (sortexp != "")
- {
- DV.Sort = sortexp;
- }
- GridView1.DataSource = DV;
- GridView1.DataBind();
- }
- protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
- {
- if (ViewState["orderby"].ToString() == "ASC")
- {
- ViewState["orderby"] = "DESC";
- ViewState["sortexp"] = e.SortExpression + " " + " DESC";
- }else
- {
- ViewState["orderby"] = "ASC";
- ViewState["sortexp"] = e.SortExpression + " " + " ASC";
- }
- Sort(ViewState["sortexp"].ToString());
- }