解析GridView自带分页及与DropDownList结合使用
GridView自带的分页功能实现:
要实现GrdView分页的功能
操作如下:
- 更改GrdView控件的AllowPaging属性为true。
- 更改GrdView控件的PageSize属性为任意数值(默认为10)
- 更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式。
GridView属性设置好了,从页面上也能看到分页样式。
现在开始实现分页的功能:
- 在<<asp:GridViewID=......>后添加,OnPageIndexChanging="GridView1_PageIndexChanging"
- 在对应的aspx.cs中添加:
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse) { GridView1.PageIndex=e.NewPageIndex; InitPage();//重新绑定GridView数据的函数 }
参考代码:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="gridview_zidaifenye.aspx.cs"Inherits="gridview_zidaifenye"%> <!DOCTYPEhtml> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title></title> </head> <body> <formid="form1"runat="server"> <div> <asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True" AutoGenerateColumns="False"DataKeyNames="ID"PagerSettings-Mode="Numeric" OnRowDataBound="GridView1_RowDataBound"PageSize="5"OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:BoundFieldDataField="ID"HeaderText="ID"InsertVisible="False"ReadOnly="True" SortExpression="ID"/> <asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name"/> <asp:BoundFieldDataField="Stock"HeaderText="Stock"SortExpression="Stock"/> </Columns> </asp:GridView> 每页显示<asp:DropDownListID="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:LabelID="lblMsg"runat="server"></asp:Label> </div> </form> </body> </html>
后台代码:
usingSystem; usingSystem.Collections.Generic; usingSystem.Data; usingSystem.Data.SqlClient; usingSystem.Linq; usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; publicpartialclassgridview_zidaifenye:System.Web.UI.Page { DBAccessdb=newDBAccess(); protectedvoidPage_Load(objectsender,EventArgse) { if(!IsPostBack) { //gvProduct.DataSource=getData(); //gvProduct.DataBind(); BindGrid(); } } publicvoidBindGrid() { SqlCommandcomm=db.CreateCommand("select*fromproductp,Uuseruwherep.userid=u.id"); SqlDataAdaptersda=newSqlDataAdapter(); sda.SelectCommand=comm; DataSetds=newDataSet(); sda.Fill(ds,"Datatable"); DataViewdv=ds.Tables[0].DefaultView; GridView1.DataSource=dv; GridView1.DataBind(); } protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse) { GridView1.PageSize=int.Parse(DropDownList1.SelectedValue); GridView1.PageIndex=0; BindGrid();//GridView1.DataBind(); } protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse) { lblMsg.Text="当前页为第"+(GridView1.PageIndex+1).ToString()+"页,共"+(GridView1.PageCount).ToString()+"页"; } protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse) { GridView1.PageIndex=((GridViewPageEventArgs)e).NewPageIndex; BindGrid();//重新绑定GridView数据的函数 } }
总结下,方便以后用到。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!