分页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分页 属性AllowPaging="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" />属性<%--FirstPageText="首页" 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 HorizontalAlign="Center" VerticalAlign="Middle" />属性可以设置GRIDVIEW分页的样式

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

范例

前台代码

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

BorderWidth="1px" PageSize="3"

CellPadding="1" HorizontalAlign="Center" BorderStyle="None"

AllowPaging="true"

AutoGenerateColumns="False" onpageindexchanging="GvShow_PageIndexChanging"onrowdatabound="GvShow_RowDataBound" Height="132px"onrowcommand="GvShow_RowCommand">

<SelectedRowStyle ForeColor="#FFFF99"

BackColor="#FFFF99"></SelectedRowStyle>

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

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

<HeaderStyle Wrap="False" HorizontalAlign="Center" Height="24px"ForeColor="Black" VerticalAlign="Middle"

BackColor="#BFD5FA"></HeaderStyle>

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

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

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

BackColor="ActiveBorder" Font-Underline="false" />

<Columns>

<asp:TemplateField HeaderText="省份">

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="城市">

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

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

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

</Columns>

<PagerTemplate>

<table cellSpacing="0" cellPadding="0" width="100%"align="center" border="0">

<tr>

<tdvAlign="middle"align="center"> 『<asp: linkbuttonid="cmdbegin" runat="server" CommandName="begin" >首页</asp: linkbutton>』

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

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

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

</td>

<td vAlign="middle" align="center">页次 <asp: labelid="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="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:checkboxid="cmdCheck" runat="server" Text="显示数字按钮" AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged" ></asp:checkbox></td>

</tr>

</table>

</PagerTemplate>

</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["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, 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 txtAllPage = gr.FindControl ("txtAllPage") 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.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)

{

//控制页签swi tch(e.CommandName)

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

; break;case "before":if(this.GvShow.PageIndex > 0)

{this.GvShow.PageIndex -= 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;

}

//控制按钮

InitButtons() ;

}

private void InitButtons()

{

//获取gridviewrows的PagerTemplate底部模板

GridViewRow 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 ("cmdend") 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 ("txtNowRed") 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;cmdafter.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.GvShow.PageCount-1)

{cmdafter.Enabled = false;cmdend.Enabled = false;

}

}

}

}

}

此外还可以不利用GridView自带的分页模板标记<PagerTemplate></PagerTemplate>可以在GridView的外部构造一个talbe与gridview对应这样更加灵活也应用于div中的GridView。

前台代码

<table id="tablePager" runat="server" cellSpacing="0" cellPadding="0" width="100%"align="center" border="0">

<tr>

<tdvAlign="middle"align="center"> 『<asp: linkbuttonid="cmdbegin" runat="server" CommandName="begin" OnCommand="PagerButton_Command" >首页</asp: linkbutton>』

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

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

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

</td>

<td vAlign="middle" align="center">页次 <asp: labelid="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="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:checkboxid="cmdCheck" runat="server" Text="显示数字按钮" AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged" ></asp:checkbox></td>

</tr>

</table>

这俩种方式的区别在于适用内部的分页模板标记在触发事件后不用重新绑定GridView这个是由GridView内部的分页机制自动完成的而采用外部构造的方式在执行完之后必须重新绑定gridview

因为这个时候的pageIndex等信息已经变化需要重新进行绑定。

后台代码protected void PagerButton_Command(object sender, System.Web.UI.WebControls.CommandEventArgse)

{swi tch (e.CommandName)

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

; break;case "before":if (this.GvShow.PageIndex > 0)

{this.GvShow.PageIndex -= 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() ;

}

一个很好样式的假分页功能齐全

前台页面:

<table>

<tr>

<td>

<asp:GridView id="GvShow" runat="server" Width="910px"

BorderColor="#687BC4"

BorderWidth="1px" PageSize="3"

CellPadding="1" HorizontalAlign="Center" BorderStyle="None"AllowPaging="true"

AutoGenerateColumns="False"onrowdatabound="GvShow_RowDataBound" Height="132px" >

<SelectedRowStyle ForeColor="#FFFF99"

BackColor="#FFFF99"></SelectedRowStyle>

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

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

<HeaderStyle Wrap="False" HorizontalAlign="Center" Height="24px"ForeColor="Black" VerticalAlign="Middle"

BackColor="#BFD5FA"></HeaderStyle>

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

BackColor="ActiveBorder" Font-Underline="false" />

<PagerSettings Mode="Numeric" Position="Bottom"

Visible="false" />

<Columns>

<asp:TemplateField HeaderText="省份">

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="城市">

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

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

<ItemStyle HorizontalAlign="Center"

VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</td>

</tr>

<tr>

<td>

<table id="tablePager"runat="server" cellSpacing="0" cellPadding="0"width="100%" align="center" border="0">

<tr>

BuyVM($5/月)不限流量流媒体优化VPS主机 1GB内存

BuyVM商家属于比较老牌的服务商,早年有提供低价年付便宜VPS主机还记得曾经半夜的时候抢购的。但是由于这个商家风控非常严格,即便是有些是正常的操作也会导致被封账户,所以后来陆续无人去理睬,估计被我们风控的抢购低价VPS主机已经手足无措。这两年商家重新调整,而且风控也比较规范,比如才入手他们新上线的流媒体优化VPS主机也没有不适的提示。目前,BuyVM商家有提供新泽西、迈阿密等四个机房的VPS主机...

BGP.TO日本和新加坡服务器进行促销,日本服务器6.5折

BGP.TO目前针对日本和新加坡服务器进行促销,其中日本东京服务器6.5折,而新加坡服务器7.5折起。这是一家专门的独立服务器租售网站,提供包括中国香港、日本、新加坡和洛杉矶的服务器租用业务,基本上都是自有硬件、IP资源等,国内优化直连线路,机器自动化部署上架,并提供产品的基本管理功能(自助开关机重启重装等)。新加坡服务器 $93.75/月CPU:E3-1230v3内存:16GB硬盘:480GB ...

MineServer:洛杉矶CN2 GIA VPS/512MB内存/20GB NVME/800GB流量/200Mbps/KVM,58元/季

mineserver怎么样?mineserver是一家国人商家,主要提供香港CN2 KVM VPS、香港CMI KVM VPS、日本CN2 KVM VPS、洛杉矶cn2 gia端口转发等服务,之前介绍过几次,最近比较活跃。这家新推出了洛杉矶CN2 GIA VPS,512MB内存/20GB NVME/800GB流量/200Mbps/KVM,58元/季,并且进行了带宽升级,同时IP更改为美国IP。点击...

gridview分页为你推荐
手游运营手册和平精英打到王者有什么要求微信如何建群微信怎么建立群网站运营我想成为网站运营的人我该学什么??网站运营网络运营具体做什么呢网站联盟百度网盟是什么,怎么加入数码资源网安卓有没有可以离线刷题的软件?今日热点怎么删除youku今日热点怎么卸载云挂机有免费的云挂机软件吗?srv记录SRV记录的简介网络虚拟机VMware虚拟机三种网络模式的区别有哪些?
租服务器价格 vps是什么意思 com域名抢注 国外代理服务器软件 空间合租 metalink 国外视频网站有哪些 512mb 便宜空间 空间申请 卡巴斯基试用版下载 netvigator sonya 留言板 免费php空间申请 天鹰抗ddos防火墙 电脑主机响 流媒体服务器软件 qq空间登入 更多