分页GridView分页系列(精装版)

gridview分页  时间:2021-02-11  阅读:()

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 行索引

设置普通的 GridView分页属性Al lowPaging="True" 、 PageSize="2"设置分页事件onpageindexchanging="GvShow_PageIndexChanging"

后台方法绑定protected void GvShow_PageIndexChanging(object sender,GridViewPageEventArgs e)

{this GvShow PageIndex=e NewPageIndex;

BindView();

}

2  自定义样式的GridView自带分页

普通的GridView自带的分页不带样式只是普通的1 ,2,3等如果希望获取到具有其他分页样式就应该设置<PagerSettings Position="TopAndBottom" PageButtonCount="1"/>属性<%--Fi rstPageText="首页"LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页

"--%>

后台访问此属性的实例this GvShow PagerSettings FirstPageText="首页";this GvShow PagerSettings LastPageText="尾页";this GvShow PagerSettings NextPageText="下一页";this GvShow PagerSettings PreviousPageText="上一页";this GvShow PagerSettings Mode=PagerButtons NextPreviousFirstLast;

通过<PagerStyle HorizontalAl ign="Center" VerticalAl ign="Middle" />属性可以设置GRIDVIEW分页的样式

3 在<Pag e rTem pl ate></Page rTem p late>分页模板中自定义分页的样式虽然微软开辟了这个模板提供给用户类似于自定义分页的功能但这个功能完全还是基于微软的GridView自带的分页进行的 <PagerSettings>属性的 Visable 的属性必须是true Al lowPaging="true"与PageSize="3"属性页都要进行相关的有效设置才可以。这种情况下的分页可以不使用onpageindexchanging="GvShow_PageIndexChanging"微软自带的分页事件开发自己独立的分页事件与方法即可

例

前台代码

<asp:GridView id="GvShow" runat="server"Width="910px"BorderColor="#687BC4"

BorderWidth="1px"PageSize="3"

Cel lPadding="1"HorizontalAl ign="Center"BorderStyle="None"

Al lowPaging="true"

AutoGenerateColum ns="False"onpageindexchanging="GvShow_PageIndexChanging"onrowdatabound="GvShow_RowDataBound"Height="132px"onrowcomm and="GvShow_RowComm and">

<SelectedRowStyle ForeColor="#FFFF99"

BackColor="#FFFF99"></SelectedRowStyle>

<AlternatingRowStyle BackColor="#EEF2F1"></AlternatingRowStyle>

<RowStyle BackColor="White"Height="24"></RowStyle>

<HeaderStyle Wrap="False"HorizontalAl ign="Center"Height="24px"ForeColor="Black"VerticalAl ign="Middle"

BackColor="#BFD5FA"></HeaderStyle>

<%--<PagerSettings Position="TopAndBottom"/>--%>

<%--FirstPageText="首页"LastPageText="尾页" NextPageText="下一页" PreviousPageText="上一页"--%>

<PagerStyle ForeColor="White"HorizontalAlign="Center"

BackColor="ActiveBorder"Font-Underl ine="false" />

<Columns>

<asp:TemplateField HeaderText="省份">

<ItemStyle HorizontalAl ign="Center"

VerticalAlign="Middle"></ItemStyle>

<Item Tem plate>

<asp:Label ID="lblRegionName" runat="server"Text='<%#Eval("rnam e")%>'></asp:Label>

</Item Template>

</asp:Tem plateField>

<asp:TemplateField HeaderText="城市">

<ItemStyle HorizontalAl ign="Center"

VerticalAlign="Middle"></ItemStyle>

<Item Tem plate>

<asp:Label ID="lblCityName" runat="server"Text='<%#Eval("cnam e")%>'></asp:Label>

</Item Template>

</asp:Tem plateField>

<asp:TemplateField HeaderText="用户名">

<ItemStyle HorizontalAl ign="Center"

VerticalAlign="Middle"></ItemStyle>

<Item Tem plate>

<asp:Label ID="lblUserName" runat="server"Text='<%#Eval("usernam e")%>'></asp:Label>

</Item Template>

</asp:Tem plateField>

</Columns>

<PagerTem plate>

<table cellSpacing="0"cel lPadding="0"width="100%"al ign="center"border="0">

<tr>

<td vAl ign="middle"align="center"> 『<asp: l inkbuttonid="cmdbegin" runat="server"CommandName="begin" >首页</asp: l inkbutton>』

『<asp: l inkbutton id="cmdbefore" runat="server"CommandName="before">上一页</asp: l inkbutton>』

『<asp: l inkbutton id="cmdafter" runat="server"CommandName="after" >下一页</asp: l inkbutton>』

『<asp: l inkbutton id="cmdend" runat="server"CommandName="end" >尾页</asp: linkbutton>』

</td>

<td vAl ign="middle"align="center">页次 <asp: labelid="txtNowPage" runat="server"ForeColor="red">0</asp: label>/<asp: label id="txtAl lPage"runat="server"ForeColor="red">0</asp: label>页&nbsp;

共<asp: label id="txtTotal" runat="server"ForeColor="red">0</asp: label>条记录&n bsp;

<asp: label id="txtNowRed" runat="server"ForeColor="red">0</asp: label>条记录/页

</td>

<td vAl ig n="top"al ig n="center"><asp:checkbox

id="cmdCheck" runat="server"Text="显示数字按钮"AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged"></asp:checkbox></td></tr>

</ta b l e>

</PagerTem plate>

</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();

I n itButto ns();

}

}private void BindView()

{

DataTable dt=UserDemoAccess GetUserSouce();

ViewState["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();

I n itButton s();

}protected void GvShow_RowDataBound(object sender,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("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 txtAl lPage=gr FindControl("txtAl lPage")as Label ;// //总记录数

// Label txtTotal=gr FindControl("txtTotal")as Label ;// //条记录/页

// Label txtNowRed=gr FindControl("txtNowRed")as Label ;// txtNowPage Text=(this GvShow PageIndex+1)ToString();// txtAl lPage Text=this GvShow PageCount ToString();// txtTotal 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")asCheckBox;

//if(cmdCheck Checked)

//{

// this GvShow PagerSettings Mode=PagerButtons Numeric;

//}

}protected void GvShow_RowCommand(object sender,GridViewCommandEventArgs e)

{

//控制页签switch(e CommandName)

{case"begin":this GvShow PageIndex=0;

10gbiz首月半价月付2.36美元,香港/洛杉矶VPS、硅谷独立服务器/站群服务器

收到10gbiz发来的7月份优惠方案,中国香港、美国洛杉矶机房VPS主机4折优惠码,优惠后洛杉矶VPS月付2.36美元起,香港VPS月付2.75美元起。这是一家2020年成立的主机商,提供的产品包括独立服务器租用和VPS主机等,数据中心在美国洛杉矶、圣何塞和中国香港。商家VPS主机基于KVM架构,支持使用PayPal或者支付宝付款。洛杉矶VPS架构CPU内存硬盘带宽系统价格单核512MB10GB1...

HostNamaste$24 /年,美国独立日VPS优惠/1核1G/30GB/1Gbps不限流量/可选达拉斯和纽约机房/免费Windows系统/

HostNamaste是一家成立于2016年3月的印度IDC商家,目前有美国洛杉矶、达拉斯、杰克逊维尔、法国鲁贝、俄罗斯莫斯科、印度孟买、加拿大魁北克机房。其中洛杉矶是Quadranet也就是我们常说的QN机房(也有CC机房,可发工单让客服改机房);达拉斯是ColoCrossing也就是我们常说的CC机房;杰克逊维尔和法国鲁贝是OVH的高防机房。采用主流的OpenVZ和KVM架构,支持ipv6,免...

青云互联:香港安畅CN2弹性云限时首月五折,15元/月起,可选Windows/可自定义配置

青云互联怎么样?青云互联是一家成立于2020年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、韩国服务器、香港服务器、美国云服务器,香港安畅cn2弹性云限时首月五折,15元/月起;可选Windows/可自定义配置,让您的网站高速、稳定运行。点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联香港云服务器活动...

gridview分页为你推荐
外网和内网什么是内网,和外网有什么区别邮箱打不开怎么办我的邮箱打不开怎么办百度抢票浏览器猎豹浏览器,360抢票,百度卫士抢票哪个抢票工具好?qq空间装扮qq空间的装扮空间在哪?网易公开课怎么下载哪位高手指导一下,如何下载网易公开课啊?神雕侠侣礼包大全神雕侠侣手游每天送的元宝买什么合适数据库损坏数据库坏了,怎么修复?xp系统停止服务xp系统停止服务怎么办?免费免费建站我想建一个自己的免费网站,但不知道那里有..如何快速收录如何做到让百度快速收录
美国免费虚拟主机 快速域名备案 hostigation 西安电信测速 香港cdn 鲜果阅读 qq数据库 hnyd 骨干网络 vip购优惠 1美金 网站在线扫描 创建邮箱 域名与空间 我的世界服务器ip net空间 北京主机托管 rewritecond 国外免费网盘 japanese50m咸熟 更多