如何分析ASP.NET GridView分页与双向排序
短信预约 -IT技能 免费直播动态提醒
本篇文章为大家展示了如何分析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分页与双向排序,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341