ASP.NET GridView分页与双向排序案例

开发 后端
ASP.NET GridView分页是个经常需要用到的功能。本文介绍一个ASP.net的GridView分页,双向排序,带指示箭头图标案例。

以下代码提供一个ASP.NET GridView分页,双向排序,带指示箭头图标案例。

GridViewPage.aspx(才用后台代码绑定方式)

  1. < form id="form1" runat="server"> 
  2.     < asp:GridView AutoGenerateColumns"False " AllowPaging="true" AllowSorting="true"   
  3.                    PageSize="5" ID="gvData" runat="server" BackColor="White" BorderColor="#336666"   
  4.                    BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"   
  5.                    SkinID="GridViewSkin" OnPageIndexChanging="gvData_PageIndexChanging"   
  6.                    OnSorting="gvData_Sorting" OnRowCreated"gvData_RowCreated" > 
  7.      < Columns> 
  8.         < asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name"/> 
  9.         < asp:BoundField DataField="time" HeaderText="时间" SortExpression="time"/> 
  10.         < asp:BoundField DataField="title" HeaderText="标题" SortExpression="title"/> 
  11.         < asp:BoundField DataField="content" HeaderText="正文" SortExpression="content"/> 
  12.       < /Columns> 
  13.     < /asp:GridView> 
  14. < /form> 
  15.  
  16.  

ASP.NET GridView分页代码:GridViewPage.aspx.cs

  1. public partial class GridViewPage : System.Web.UI.Page  
  2. {  
  3.     SqlDataAdapter da = null;  
  4.     DataSet ds = null;  
  5.     private string gvSortExpression  
  6.     {  
  7.         get  
  8.         {  
  9.             if (ViewState["sortorder"] != null && ViewState["sortorder"].ToString() != string.Empty)  
  10.             {  
  11.                 return ViewState["sortorder"].ToString();  
  12.             }  
  13.             else  
  14.             {  
  15.                 return "";  
  16.             }  
  17.         }  
  18.         set  
  19.         {  
  20.             ViewState["sortorder"] = value;  
  21.         }  
  22.     }   
  23.  
  24.     protected void Page_Load(object sender, EventArgs e)  
  25.     {  
  26.         if (!Page.IsPostBack)  
  27.         {  
  28.             ViewState["sortorder"] = "name";  
  29.             ViewState["orderdire"] = "asc";  
  30.             Bind();  
  31.         }  
  32.     }  
  33.     protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  34.     {  
  35.         gvData.PageIndex = e.NewPageIndex;  
  36.         Bind();       
  37.     }  
  38.  
  39.     protected void gvData_Sorting(object sender, GridViewSortEventArgs e)  
  40.     {  
  41.         gvSortExpression = e.SortExpression;  
  42.         string spage = e.SortExpression;  
  43.         if (ViewState["sortorder"].ToString() == spage)  
  44.         {  
  45.             if (ViewState["orderdire"].ToString() == "desc")  
  46.                 ViewState["orderdire"] = "asc";  
  47.             else  
  48.                 ViewState["orderdire"] = "desc";  
  49.         }  
  50.         else  
  51.         {  
  52.             ViewState["sortorder"] = e.SortExpression;  
  53.         }  
  54.         Bind();  
  55.     }  
  56.  
  57.     private void Bind()  
  58.     {  
  59.         SqlConnection con = new SqlConnection(BookSystem.SQLSERVERCONNECTIONSTRING);  
  60.         string sql = "select name,time,title,content from guestbook";  
  61.         da = new SqlDataAdapter(sql, con);  
  62.         con.Open();  
  63.         ds = new DataSet();  
  64.         da.Fill(ds);  
  65.         DataView view = ds.Tables[0].DefaultView;  
  66.         string sort = (string)ViewState["sortorder"] + " " + (string)ViewState["orderdire"];  
  67.         view.Sortsort;  
  68.         gvData.DataSource = view;  
  69.         gvData.DataBind();  
  70.         con.Close();  
  71.  
  72.     }  
  73.     protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)  
  74.     {  
  75.         for (int index = 0; index < = gvData.Columns.Count - 1; index++)  
  76.         {  
  77.             if ((gvData.Columns[index].SortExpression == gvSortExpression) && (gvData.Columns  
  78.                  [index].SortExpression != ""))  
  79.             {  
  80.                 Image img = new Image();  
  81.                 if ((string)ViewState["orderdire"] == "desc")  
  82.                 {  
  83.                     img.ImageUrl = "~/Images/sortascending.gif";  
  84.                     img.ToolTip = "递增排列";  
  85.                 }  
  86.                 else  
  87.                 {  
  88.                     img.ImageUrl = "~/Images/sortdescending.gif";  
  89.                     img.ToolTip = "递减排列";  
  90.                 }  
  91.                 gvRow.Cells[index].Controls.Add(img);  
  92.             }  
  93.         }  
  94.     }  
  95.  
  96.  
  97.     protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)  
  98.     {  
  99.         if (e.Row.RowType == DataControlRowType.Header)  
  100.         {  
  101.             ShowSortedIcon(gvData, e.Row);  
  102.         }  
  103.     }  
  104.  
  105.  
    
    

    以上就是ASP.NET GridView分页,双向排序及带指示箭头图标的案例。本文来自梦的彼岸的博客:《ASP.net的GridView分页,双向排序,带指示箭头图标案例》。

    【编辑推荐】

    1. ASP.NET分页代码的实现(C#,FCKeditor)
    2. 浅析ASP.NET的TypeResolver
    3. ASP.NET中定义JavaScriptConverter
    4. 在ASP.NET中替换Sys.Services的方法
    5. 使用ASP.NET AJAX的Profile Service
    责任编辑:周立方 来源: 梦的彼岸
    相关推荐

    2009-07-24 16:15:00

    扩展ASP.NET G

    2009-07-24 15:35:00

    ASP.NET Gri

    2009-08-03 18:29:31

    GridView与Da

    2010-03-19 09:17:16

    ASP.NET MVC

    2009-07-27 16:22:54

    GridView选择行

    2009-07-27 15:58:25

    添加GridView

    2009-07-27 16:09:05

    GridView显示数

    2009-07-27 10:12:37

    GridView和Ob

    2012-04-13 10:05:24

    ASP.NET

    2012-04-23 15:10:18

    ASP.NET

    2009-07-22 16:02:39

    ASP.NET MVCPagedList

    2009-07-28 14:47:18

    ASP.NET MVC

    2009-07-20 18:02:48

    GridView控件ASP.NET 2.0

    2012-04-09 10:34:21

    jQuery

    2009-07-28 03:42:00

    ASP.NET Gri

    2009-08-04 14:23:36

    ASP.NET查询分页

    2009-11-06 09:23:41

    ASP.NET高效分页

    2009-09-10 09:50:47

    ASP.NET MVC

    2009-12-02 09:07:45

    ASP.NET 4.0

    2009-03-09 13:46:31

    RoutingWebASP.NET
    点赞
    收藏

    51CTO技术栈公众号