本篇文章为大家展示了如何分析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分页与双向排序,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。