分页系列精装版_第1页
分页系列精装版_第2页
分页系列精装版_第3页
分页系列精装版_第4页
分页系列精装版_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、GridView分页系列(精装版)1:GridView自带分页:GridView自带的分页,是假分页,他每次从数据库把数据全部查询出之后,通过分页的算法,进行按每页数量进行分页。分页的属性元素:分页功能的实现就是通过对这些属性元素的操作实现的。/this.GvShow.PageIndex 当前页的索引/this.GvShow.PageCount 总共的页数/this.GvShow.Rows.Count 当前页签内的gridview的行数/this.GvShow.PageSize 每页的记录数/this.GvShow.PageIndex*this.GvShow.rows.count + 1 行索

2、引设置普通的GridView分页:属性AllowPaging="True"、PageSize="2" 设置分页事件onpageindexchanging="GvShow_PageIndexChanging"后台方法绑定:protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)this.GvShow.PageIndex = e.NewPageIndex;BindView();2:自定义样式的GridView自带分页:普通的GridVie

3、w自带的分页,不带样式,只是普通的1,2,3等,如果希望获取到具有其他分页样式就应该设置<PagerSettings Position="TopAndBottom" PageButtonCount="1" />属性<%-FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"-%>后台访问此属性的实例:this.GvShow.PagerSettings.

4、FirstPageText = "首页"this.GvShow.PagerSettings.LastPageText = "尾页"this.GvShow.PagerSettings.NextPageText = "下一页"this.GvShow.PagerSettings.PreviousPageText = "上一页"this.GvShow.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;通过<PagerStyle HorizontalAlig

5、n="Center" VerticalAlign="Middle" />属性可以设置GRIDVIEW分页的样式3:在<PagerTemplate></PagerTemplate>分页模板中自定义分页的样式,虽然微软开辟了这个模板提供给用户类似于自定义分页的功能,但这个功能完全还是基于微软的GridView自带的分页进行的,<PagerSettings>属性的Visable的属性必须是true AllowPaging="true" 与 PageSize="3"属性页都要进行相

6、关的有效设置才可以。这种情况下的分页可以不使用onpageindexchanging="GvShow_PageIndexChanging"微软自带的分页事件,开发自己独立的分页事件与方法即可范例:前台代码:<asp:GridView id="GvShow" runat="server" Width="910px" BorderColor="#687BC4" BorderWidth="1px" PageSize="3"CellPadding="

7、;1" HorizontalAlign="Center" BorderStyle="None" AllowPaging="true" AutoGenerateColumns="False" onpageindexchanging="GvShow_PageIndexChanging" onrowdatabound="GvShow_RowDataBound" Height="132px" onrowcommand="GvShow_RowCo

8、mmand"><SelectedRowStyle ForeColor="#FFFF99" BackColor="#FFFF99"></SelectedRowStyle><AlternatingRowStyle BackColor="#EEF2F1"></AlternatingRowStyle><RowStyle BackColor="White" Height="24" ></RowStyle><Hea

9、derStyle Wrap="False" HorizontalAlign="Center" Height="24px" ForeColor="Black" VerticalAlign="Middle"BackColor="#BFD5FA"></HeaderStyle><%-<PagerSettings Position="TopAndBottom" />-%><%-FirstPageText="首

10、页" LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"-%> <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="ActiveBorder" Font-Underline="false" /><Columns> <asp:TemplateField HeaderT

11、ext="省份"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <asp:Label ID="lblRegionName" runat="server" Text='<%#Eval("rname") %>' ></asp:Label></ItemTemp

12、late></asp:TemplateField> <asp:TemplateField HeaderText="城市"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <asp:Label ID="lblCityName" runat="server" Text='<%#Eval(&qu

13、ot;cname") %>' ></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="用户名"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <asp:Label ID="lblUs

14、erName" runat="server" Text='<%#Eval("username") %>'></asp:Label></ItemTemplate></asp:TemplateField></Columns><PagerTemplate> <table cellSpacing="0" cellPadding="0" width="100%" align="cente

15、r" border="0"> <tr> <td vAlign="middle" align="center"><asp:linkbutton id="cmdbegin" runat="server" CommandName="begin" >首页</asp:linkbutton> <asp:linkbutton id="cmdbefore" runat="server"

16、; CommandName="before" >上一页</asp:linkbutton> <asp:linkbutton id="cmdafter" runat="server" CommandName="after" >下一页</asp:linkbutton> <asp:linkbutton id="cmdend" runat="server" CommandName="end" >尾页</as

17、p:linkbutton> </td> <td vAlign="middle" align="center">页次:<asp:label id="txtNowPage" runat="server" ForeColor="red">0</asp:label>/<asp:label id="txtAllPage" runat="server" ForeColor="red">

18、;0</asp:label>页&nbsp; 共<asp:label id="txtTotal" runat="server" ForeColor="red">0</asp:label>条记录&nbsp; <asp:label id="txtNowRed" runat="server" ForeColor="red">0</asp:label>条记录/页 </td> <td vAlig

19、n="top" align="center"><asp:checkbox id="cmdCheck" runat="server" Text="显示数字按钮" AutoPostBack="True" oncheckedchanged="cmdCheck_CheckedChanged" ></asp:checkbox></td> </tr> </table></PagerTemplate

20、></asp:GridView>后台代码:namespace GridViewDemo.GridView分页系列 public partial class GridView自定义分页02 : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) BindView(); InitButtons(); private void BindView() DataTable dt = UserDemoAccess.GetUserSouce(); ViewState

21、"RowCounts" = dt.Rows.Count.ToString(); this.GvShow.DataSource = dt; this.GvShow.DataBind(); protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) this.GvShow.PageIndex = e.NewPageIndex; BindView(); InitButtons(); protected void GvShow_RowDataBound(object sender,

22、GridViewRowEventArgs e) if (e.Row.RowType = DataControlRowType.DataRow) e.Row.Attributes.Add("onmouseover", "this.setAttribute('BKC',this.style.backgroundColor);this.style.cursor='default',this.style.backgroundColor='#ffff99'"); e.Row.Attributes.Add("

23、onmouseout", "this.style.backgroundColor=this.getAttribute('BKC');"); /if(e.Row.RowType = DataControlRowType.Pager) / / GridViewRow gr = e.Row; / /页次 - 第几页 / Label txtNowPage = gr.FindControl("txtNowPage") as Label; / /总页数 / Label txtAllPage = gr.FindControl("tx

24、tAllPage") as Label; / /总记录数 / Label txtTotal = gr.FindControl("txtTotal") as Label; / /条记录/页 / Label txtNowRed = gr.FindControl("txtNowRed") as Label; / txtNowPage.Text = (this.GvShow.PageIndex+1).ToString(); / txtAllPage.Text = this.GvShow.PageCount.ToString(); / txtTotal.

25、Text = ViewState"RowCounts".ToString(); / txtNowRed.Text = this.GvShow.PageSize.ToString(); / protected void cmdCheck_CheckedChanged(object sender, EventArgs e) /CheckBox cmdCheck = this.GvShow.BottomPagerRow.FindControl("cmdCheck") as CheckBox; /if (cmdCheck.Checked) / / this.Gv

26、Show.PagerSettings.Mode = PagerButtons.Numeric; / protected void GvShow_RowCommand(object sender, GridViewCommandEventArgs e) /控制页签 switch(e.CommandName) case "begin": this.GvShow.PageIndex = 0; ; break; case "before": if(this.GvShow.PageIndex > 0) this.GvShow.PageIndex -= 1;

27、; break; case "after": if(this.GvShow.PageIndex < this.GvShow.PageCount - 1) this.GvShow.PageIndex += 1; ; break; case "end": this.GvShow.PageIndex = this.GvShow.PageCount-1; ; break; /控制按钮 InitButtons(); private void InitButtons() /获取gridviewrows的PagerTemplate底部模板 GridViewRow

28、 gr = this.GvShow.BottomPagerRow; LinkButton cmdbegin = gr.FindControl("cmdbegin") as LinkButton; LinkButton cmdbefore = gr.FindControl("cmdbefore") as LinkButton; LinkButton cmdafter = gr.FindControl("cmdafter") as LinkButton; LinkButton cmdend = gr.FindControl("c

29、mdend") as LinkButton; /页次 - 第几页 Label txtNowPage = gr.FindControl("txtNowPage") as Label; /总页数 Label txtAllPage = gr.FindControl("txtAllPage") as Label; /总记录数 Label txtTotal = gr.FindControl("txtTotal") as Label; /条记录/页 Label txtNowRed = gr.FindControl("txtNo

30、wRed") as Label; txtNowPage.Text = (this.GvShow.PageIndex + 1).ToString(); txtAllPage.Text = this.GvShow.PageCount.ToString(); txtTotal.Text = ViewState"RowCounts".ToString(); txtNowRed.Text = this.GvShow.PageSize.ToString(); cmdbegin.Enabled = false; cmdbefore.Enabled = false; cmdaft

31、er.Enabled = false; cmdend.Enabled = false; if(this.GvShow.PageCount > 1) cmdbegin.Enabled = true; cmdbefore.Enabled = true; cmdafter.Enabled = true; cmdend.Enabled = true; if(this.GvShow.PageIndex = 0) cmdbegin.Enabled = false; cmdbefore.Enabled = false; else if(this.GvShow.PageIndex = this.GvSh

32、ow.PageCount-1) cmdafter.Enabled = false; cmdend.Enabled = false; 此外还可以不利用GridView自带的分页模板标记<PagerTemplate></PagerTemplate>可以在GridView的外部构造一个talbe与gridview对应,这样更加灵活,也应用于div中的GridView。前台代码:<table id="tablePager" runat="server" cellSpacing="0" cellPadding=&q

33、uot;0" width="100%" align="center" border="0"> <tr> <td vAlign="middle" align="center"><asp:linkbutton id="cmdbegin" runat="server" CommandName="begin" OnCommand="PagerButton_Command" >

34、;首页</asp:linkbutton> <asp:linkbutton id="cmdbefore" runat="server" CommandName="before" OnCommand="PagerButton_Command" >上一页</asp:linkbutton> <asp:linkbutton id="cmdafter" runat="server" CommandName="after" On

35、Command="PagerButton_Command" >下一页</asp:linkbutton> <asp:linkbutton id="cmdend" runat="server" CommandName="end" OnCommand="PagerButton_Command" >尾页</asp:linkbutton> </td> <td vAlign="middle" align="center

36、">页次:<asp:label id="txtNowPage" runat="server" ForeColor="red">0</asp:label>/<asp:label id="txtAllPage" runat="server" ForeColor="red">0</asp:label>页&nbsp; 共<asp:label id="txtTotal" runat=&qu

37、ot;server" ForeColor="red">0</asp:label>条记录&nbsp; <asp:label id="txtNowRed" runat="server" ForeColor="red">0</asp:label>条记录/页 </td> <td vAlign="top" align="center"><asp:checkbox id="cmdCheck

38、" runat="server" Text="显示数字按钮" AutoPostBack="True" oncheckedchanged="cmdCheck_CheckedChanged" ></asp:checkbox></td> </tr> </table>这俩种方式的区别在于:适用内部的分页模板标记,在触发事件后,不用重新绑定GridView,这个是由GridView内部的分页机制自动完成的,而采用外部构造的方式,在执行完之后,必须重新绑定grid

39、view,因为这个时候的pageIndex等信息已经变化,需要重新进行绑定。后台代码:protected void PagerButton_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e) switch (e.CommandName) case "begin": this.GvShow.PageIndex = 0; ; break; case "before": if (this.GvShow.PageIndex > 0) this.GvShow.PageInd

40、ex -= 1; ; break; case "after": if (this.GvShow.PageIndex < this.GvShow.PageCount - 1) this.GvShow.PageIndex += 1; ; break; case "end": this.GvShow.PageIndex = this.GvShow.PageCount - 1; ; break; /在设置完页签后,将数据源重新绑定到GridView中 BindView(); InitButtons(); 一个很好样式的假分页,功能齐全:前台页面:<t

41、able> <tr> <td> <asp:GridView id="GvShow" runat="server" Width="910px" BorderColor="#687BC4" BorderWidth="1px" PageSize="3" CellPadding="1" HorizontalAlign="Center" BorderStyle="None" AllowPag

42、ing="true" AutoGenerateColumns="False" onrowdatabound="GvShow_RowDataBound" Height="132px" ><SelectedRowStyle ForeColor="#FFFF99" BackColor="#FFFF99"></SelectedRowStyle><AlternatingRowStyle BackColor="#EEF2F1">

43、;</AlternatingRowStyle><RowStyle BackColor="White" Height="24" ></RowStyle><HeaderStyle Wrap="False" HorizontalAlign="Center" Height="24px" ForeColor="Black" VerticalAlign="Middle"BackColor="#BFD5FA"

44、></HeaderStyle> <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="ActiveBorder" Font-Underline="false" /><PagerSettings Mode="Numeric" Position="Bottom" Visible="false" /> <Columns> <a

45、sp:TemplateField HeaderText="省份"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <asp:Label ID="lblRegionName" runat="server" Text='<%#Eval("rname") %>' ></as

46、p:Label></ItemTemplate></asp:TemplateField> <asp:TemplateField HeaderText="城市"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <asp:Label ID="lblCityName" runat="server"

47、Text='<%#Eval("cname") %>' ></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="用户名"><ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle><ItemTemplate> <

48、asp:Label ID="lblUserName" runat="server" Text='<%#Eval("username") %>'></asp:Label></ItemTemplate></asp:TemplateField></Columns></asp:GridView> </td> </tr> <tr> <td> <table id="tablePager&qu

49、ot; runat="server" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0"> <tr> <td vAlign="middle" align="center"><asp:linkbutton id="cmdbegin" runat="server" Comman

50、dName="begin" OnCommand="PagerButton_Command" >首页</asp:linkbutton> <asp:linkbutton id="cmdbefore" runat="server" CommandName="before" OnCommand="PagerButton_Command" >上一页</asp:linkbutton> <asp:linkbutton id="cmd

51、after" runat="server" CommandName="after" OnCommand="PagerButton_Command" >下一页</asp:linkbutton> <asp:linkbutton id="cmdend" runat="server" CommandName="end" OnCommand="PagerButton_Command" >尾页</asp:linkbutto

52、n> </td> <td vAlign="middle" align="center">页次:<asp:label id="txtNowPage" runat="server" ForeColor="red">0</asp:label>/<asp:label id="txtAllPage" runat="server" ForeColor="red">0</asp:

53、label>页&nbsp; 共<asp:label id="txtTotal" runat="server" ForeColor="red">0</asp:label>条记录&nbsp; <asp:label id="txtNowRed" runat="server" ForeColor="red">0</asp:label>条记录/页 </td> <td> <asp:Link

54、Button ID="lbtnTurnNewPage" runat="server" Text="跳转" onclick="lbtnTurnNewPage_Click"></asp:LinkButton> <asp:TextBox ID="txtTurn" runat="server" Width="20" Height="20"></asp:TextBox> </td> <t

55、d> <asp:DropDownList ID="drpPageNumbers" runat="server" AutoPostBack="true" onselectedindexchanged="drpPageNumbers_SelectedIndexChanged"> </asp:DropDownList> </td> </tr> </table> </td> </tr> </table>后台页面:namespace GridViewDemo.GridView分页系列 public partial class GridView自定义分页04 : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) if (!IsPost

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论