如果你在GridView控件上设置 AllowPaging="true" or AllowSorting="true" 而没有使用使用数据源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),运行则会出现下列错误:
当你在GridView控件上单击下一页时:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
当你点击排序时,则回出现:
The GridView 'GridViewID' fired event Sorting which wasn't handled.
不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...
你必须添加一个操作才可以排序及分页。。
public class WebHandler { private WebHandler() { } public static readonly WebHandler Instance = new WebHandler(); #region GridView Handler public string GridViewSortExpression { get { return HttpContext.Current.Session["SortExpression"] as string ?? string.Empty; } set { HttpContext.Current.Session["SortExpression"] = value; } } public string GridViewSortDirection { get { return HttpContext.Current.Session["SortDirection"] as string ?? "ASC"; } set { HttpContext.Current.Session["SortDirection"] = value; } } public string GetSortDirection() { switch (GridViewSortDirection) { case "ASC": GridViewSortDirection = "DESC"; break; case "DESC": GridViewSortDirection = "ASC"; break; } return GridViewSortDirection; } public DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging) { if (dataTable != null) { DataView dataView = new DataView(dataTable); if (GridViewSortExpression != string.Empty) { if (isPageIndexChanging) { dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection); } else { dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection()); } } return dataView; } else { return new DataView(); } } #endregion }
aspx & aspx.cs
<asp:GridView ID="gvInvoiceHistory" runat="server" AllowPaging="True" OnPageIndexChanging="gvInvoiceHistory_PageIndexChanging" OnRowCreated="gvInvoices_RowCreated" OnSorting="gvInvoiceHistory_Sorting" AllowSorting="True" AutoGenerateColumns="False" > //aspx.cs private readonly WebHandler webHandler = WebHandler.Instance; protected void gvInvoiceHistory_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), true); gvInvoiceHistory.PageIndex = e.NewPageIndex; gvInvoiceHistory.DataBind(); } protected void gvInvoiceHistory_Sorting(object sender, GridViewSortEventArgs e) { webHandler.GridViewSortExpression = e.SortExpression; int pageIndex = gvInvoiceHistory.PageIndex; gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), false); gvInvoiceHistory.DataBind(); gvInvoiceHistory.PageIndex = pageIndex; } public virtual DataTable GetInvoiceList() { DataTable result = new DataTable(); ........... return result; }
//DataBind 排序图标丢失,
protected virtual void gvInvoices_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row != null && e.Row.RowType == DataControlRowType.Header) { foreach (TableCell cell in e.Row.Cells) { if (cell.HasControls()) { LinkButton button = cell.Controls[0] as LinkButton; if (button != null) { //Label lblsort = new Label(); if (ddlParentInvoice.SelectedValue != "-1" && webHandler.GridViewSortExpression == button.CommandArgument) { Image image = new Image(); if (webHandler.GetSortDirection() == "ASC") { image.SkinID = "SortArrowDown"; //lblsort.Text = " <font>▼</font>"; } else { image.SkinID = "SortArrowUp"; //lblsort.Text = " <font>▲</font>"; } cell.Controls.Add(image); } } } } } }