这篇文章主要介绍了GridView自带分页及与DropDownList结合使用的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
GridView自带的分页功能实现:
要实现GrdView分页的功能
操作如下:
更改GrdView控件的AllowPaging属性为true。
更改GrdView控件的PageSize属性为 任意数值(默认为10)
更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式。
GridView属性设置好了,从页面上也能看到分页样式。
现在开始实现分页的功能:
在<<asp:GridView ID=......>后添加,OnPageIndexChanging="GridView1_PageIndexChanging"
在对应的aspx.cs中添加:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; InitPage(); //重新绑定GridView数据的函数 }
参考代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview_zidaifenye.aspx.cs" Inherits="gridview_zidaifenye" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" PagerSettings-Mode="Numeric" OnRowDataBound="GridView1_RowDataBound" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Stock" HeaderText="Stock" SortExpression="Stock" /> </Columns> </asp:GridView> 每页显示<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>5</asp:ListItem> <asp:ListItem>10</asp:ListItem> <asp:ListItem>15</asp:ListItem> </asp:DropDownList> 条记录 <asp:Label ID="lblMsg" runat="server"></asp:Label> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class gridview_zidaifenye : System.Web.UI.Page { DBAccess db = new DBAccess(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // gvProduct.DataSource = getData(); // gvProduct.DataBind(); BindGrid(); } } public void BindGrid() { SqlCommand comm = db.CreateCommand("select * from product p,Uuser u where p.userid=u.id"); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = comm; DataSet ds = new DataSet(); sda.Fill(ds,"Datatable"); DataView dv = ds.Tables[0].DefaultView; GridView1.DataSource = dv; GridView1.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { GridView1.PageSize = int.Parse(DropDownList1.SelectedValue); GridView1.PageIndex = 0; BindGrid(); //GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { lblMsg.Text = "当前页为第" + (GridView1.PageIndex + 1).ToString() + "页,共" + (GridView1.PageCount).ToString() + "页"; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = ((GridViewPageEventArgs)e).NewPageIndex; BindGrid(); //重新绑定GridView数据的函数 } }
感谢你能够认真阅读完这篇文章,希望小编分享的“GridView自带分页及与DropDownList结合使用的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。