如何分析ASP.NET GridView分页与双向排序

发布时间:2021-11-26 17:43:34 作者:柒染
来源:亿速云 阅读:125

本篇文章为大家展示了如何分析ASP.NET GridView分页与双向排序,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

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

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

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

public partial class GridViewPage : System.Web.UI.Page  {      SqlDataAdapter da = null;      DataSet ds = null;      private string gvSortExpression      {          get          {              if (ViewState["sortorder"] != null && ViewState["sortorder"].ToString() != string.Empty)              {                  return ViewState["sortorder"].ToString();              }              else              {                  return "";              }          }          set          {              ViewState["sortorder"] = value;          }      }        protected void Page_Load(object sender, EventArgs e)      {          if (!Page.IsPostBack)          {              ViewState["sortorder"] = "name";              ViewState["orderdire"] = "asc";              Bind();          }      }      protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)      {          gvData.PageIndex = e.NewPageIndex;          Bind();           }       protected void gvData_Sorting(object sender, GridViewSortEventArgs e)      {          gvSortExpression = e.SortExpression;          string spage = e.SortExpression;          if (ViewState["sortorder"].ToString() == spage)          {              if (ViewState["orderdire"].ToString() == "desc")                  ViewState["orderdire"] = "asc";              else                  ViewState["orderdire"] = "desc";          }          else          {              ViewState["sortorder"] = e.SortExpression;          }          Bind();      }       private void Bind()      {          SqlConnection con = new SqlConnection(BookSystem.SQLSERVERCONNECTIONSTRING);          string sql = "select name,time,title,content from guestbook";          da = new SqlDataAdapter(sql, con);          con.Open();          ds = new DataSet();          da.Fill(ds);          DataView view = ds.Tables[0].DefaultView;          string sort = (string)ViewState["sortorder"] + " " + (string)ViewState["orderdire"];          view.Sort= sort;          gvData.DataSource = view;          gvData.DataBind();          con.Close();       }      protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)      {          for (int index = 0; index < = gvData.Columns.Count - 1; index++)          {              if ((gvData.Columns[index].SortExpression == gvSortExpression) && (gvData.Columns                   [index].SortExpression != ""))              {                  Image img = new Image();                  if ((string)ViewState["orderdire"] == "desc")                  {                      img.ImageUrl = "~/Images/sortascending.gif";                      img.ToolTip = "递增排列";                  }                  else                  {                      img.ImageUrl = "~/Images/sortdescending.gif";                      img.ToolTip = "递减排列";                  }                  gvRow.Cells[index].Controls.Add(img);              }          }      }        protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)      {          if (e.Row.RowType == DataControlRowType.Header)          {              ShowSortedIcon(gvData, e.Row);          }      }    }

上述内容就是如何分析ASP.NET GridView分页与双向排序,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. asp.net中实现分页的方式有哪些
  2. ASP.NET数据绑定控件的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net gridview

上一篇:ASP.NET MVC 2.0中显示列表和详细页面的操作是怎样的

下一篇:C#如何实现基于Socket套接字的网络通信封装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》