我正在GridView中执行搜索操作并按预期方式获得结果,但是在获得结果后,如果对它们进行排序或单击分页,则会重新加载页面,并且整个网格都将发生排序和分页。应该只能用于我得到的搜索结果。
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();
Panel2.Visible = true;
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());
}