Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze ASP.NET GridView pagination and two-way sorting

Shulou Source: shulou.com Published: 2022-06-02 10:05:10 09月30日 Update

This article shows you how to analyze ASP.NET GridView pagination and two-way sorting, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The following code provides a case of ASP.NET GridView paging, bi-directional sorting, and an icon with an indication arrow.

GridViewPage.aspx (only use background code binding method)

< 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 paging code: 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.SortExpress; 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.SortExpress;} 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 = "incremental arrangement" } else {img.ImageUrl = "~ / Images/sortdescending.gif"; img.ToolTip = "decreasing arrangement";} gvRow.Cells [index] .Controls.add (img) } protected void gvData_RowCreated (object sender, GridViewRowEventArgs e) {if (e.Row.RowType = = DataControlRowType.Header) {ShowSortedIcon (gvData, e.Row);} the above is how to analyze ASP.NET GridView pagination and bi-directional sorting. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Bidirectional sorting code analysis content skills knowledge concise concise background icon that is instructions articles methods more cases arrows articles industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Microsoft MySQL Shulou Information