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

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

精品文档

GridView分页系列精装版

1 GridVi ew 自带分页 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等如果希望获取到具有其他分页样式就应该设置<PagerSettingsPosition="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:GridViewid="GvShow"runat="server"Width="910px"BorderColor="#687BC4"

BorderWidth="1px"PageSize="3"

1欢迎。下载

精品文档

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

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

<SelectedRowStyleForeColor="#FFFF99"BackColor="#FFFF99"></SelectedRowStyle>

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

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

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

BackColor="#BFD5FA"></HeaderStyle>

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

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

<PagerStyleForeColor="White"HorizontalAlign="Center"BackColor="ActiveBorder"Font-Underline="false"/>

<Columns>

<asp:TemplateFieldHeaderText="省份">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="城市">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="用户名">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

2欢迎。下载

精品文档

</Columns>

<PagerTemplate>

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

<tr>

<tdvAlign="middle"align="center">

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

『<asp: linkbuttonid="cmdbefore"runat="server"CommandName="before">上一页

</asp: linkbutton>』

『<asp: linkbuttonid="cmdafter"runat="server"CommandName="after">下一页

</asp: linkbutton>』

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

</td>

<tdvAlign="middle"align="center">页次

<asp: labelid="txtNowPage"runat="server"ForeColor="red">0</asp: label>/<asp: labelid="txtAllPage"runat="server"ForeColor="red">0</asp: label>页&nbsp;

<asp: labelid="txtTotal"runat="server"ForeColor="red">0</asp: label>条记录&nbsp;

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

</td>

<tdvAlign="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分页系列

{publicpartialclassGridView自定义分页02 : System.Web.UI.Page

{protectedvoid Page_Load(object sender, EventArgs e)

{if (!IsPostBack)

{

BindView() ;

InitButtons() ;

}

}

3欢迎。下载

精品文档privatevoid BindView()

{

DataTable dt = UserDemoAccess.GetUserSouce() ;

ViewState["RowCounts"] = dt.Rows.Count.ToString() ;this.GvShow.DataSource = dt; ;this.GvShow.DataBind() ;

}protectedvoid GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)

{this.GvShow.PageIndex = e.NewPageIndex;

BindView() ;

InitButtons() ;

}protectedvoid 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() ;

4欢迎。下载

精品文档

// txtNowRed.Text = this.GvShow.PageSize.ToString() ;

//}

}protectedvoid cmdCheck_CheckedChanged(object sender, EventArgs e)

{

//CheckBox cmdCheck = this.GvShow.BottomPagerRow.FindControl ("cmdCheck") as CheckBox;//if (cmdCheck.Checked)

//{

// this.GvShow.PagerSettings.Mode = PagerButtons.Numeric;

//}

}protectedvoid 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() ;

}

5欢迎。下载

精品文档privatevoid InitButtons()

{

//获取gridviewrows的PagerTemplate底部模板

GridViewRow gr = this.GvShow.BottomPagerRow;

LinkButton cmdbegin = gr.FindControl ("cmdbegin") asLinkButton;

LinkButton cmdbefore = gr.FindControl ("cmdbefore") asLinkButton;LinkButton cmdafter = gr.FindControl ("cmdafter") asLinkButton;

LinkButton cmdend = gr.FindControl ("cmdend") asLinkButton;

//页次--第几页

Label txtNowPage = gr.FindControl ("txtNowPage") asLabel;

//总页数

Label txtAllPage = gr.FindControl ("txtAllPage") asLabel;

//总记录数

Label txtTotal = gr.FindControl ("txtTotal") asLabel;

//条记录/页

Label txtNowRed = gr.FindControl ("txtNowRed") asLabel;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)

{

6欢迎。下载

精品文档cmdbegin.Enabled = false;cmdbefore.Enabled = false;

}elseif(this.GvShow.PageIndex == this.GvShow.PageCount-1)

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

}

}

}

}

}

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

前台代码

<tableid="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: linkbuttonid="cmdbefore"runat="server"CommandName="before"OnCommand="PagerButton_Command">上一页</asp: linkbutton>』

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

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

</td>

<tdvAlign="middle"align="center">页次

<asp: labelid="txtNowPage"runat="server"ForeColor="red">0</asp: label>/<asp: labelid="txtAllPage"runat="server"ForeColor="red">0</asp: label>页&nbsp;

<asp: labelid="txtTotal"runat="server"ForeColor="red">0</asp: label>条记录&nbsp;

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

</td>

<tdvAlign="top"align="center"><asp:checkboxid="cmdCheck"runat="server"Text="显示数字按

7欢迎。下载

精品文档

钮"AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged"></asp:checkbox></td>

</tr>

</tabl e>

这俩种方式的区别在于适用内部的分页模板标记在触发事件后不用重新绑定GridView这个是由GridView内部的分页机制自动完成的而采用外部构造的方式在执行完之后必须重新绑定gridview因为这个时候的pageIndex等信息已经变化需要重新进行绑定。

后台代码protectedvoid PagerButton_Command(object sender, System.Web.UI.WebControls.CommandEventArgs 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;

}

//在设置完页签后将数据源重新绑定到GridView中

BindView() ;

InitButtons() ;

}

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

前台页面:

<table>

<tr>

<td>

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

BorderWidth="1px"PageSize="3"

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

AutoGenerateColumns="False"

8欢迎。下载

精品文档onrowdatabound="GvShow_RowDataBound"Height="132px">

<SelectedRowStyleForeColor="#FFFF99"BackColor="#FFFF99"></SelectedRowStyle>

<AlternatingRowStyleBackColor="#EEF2F1"></AlternatingRowStyle><RowStyleBackColor="White"Height="24"></RowStyle>

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

BackColor="#BFD5FA"></HeaderStyle>

<PagerStyleForeColor="White"HorizontalAlign="Center"BackColor="ActiveBorder"Font-Underline="false"/>

<PagerSettingsMode="Numeric"Position="Bottom"Visible="false"/><Columns>

<asp:TemplateFieldHeaderText="省份">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="城市">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldHeaderText="用户名">

<ItemStyleHorizontalAlign="Center"VerticalAlign="Middle"></ItemStyle>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</td>

</tr>

<tr>

<td>

9欢迎。下载

georgedatacenter:美国VPS可选洛杉矶/芝加哥/纽约/达拉斯机房,$20/年;洛杉矶独立服务器39美元/月

georgedatacenter怎么样?georgedatacenter这次其实是两个促销,一是促销一款特价洛杉矶E3-1220 V5独服,性价比其实最高;另外还促销三款特价vps,大家可以根据自己的需要入手。georgedatacenter是一家成立于2019年的美国vps商家,主营美国洛杉矶、芝加哥、达拉斯、新泽西、西雅图机房的VPS、邮件服务器和托管独立服务器业务。georgedatacen...

百纵科技,美国独立服务器 E52670*1 32G 50M 200G防御 899元/月

百纵科技:美国高防服务器,洛杉矶C3机房 独家接入zenlayer清洗 带金盾硬防,CPU全系列E52670、E52680v3 DDR4内存 三星固态盘阵列!带宽接入了cn2/bgp线路,速度快,无需备案,非常适合国内外用户群体的外贸、搭建网站等用途。C3机房,双程CN2线路,默认200G高防,3+1(高防IP),不限流量,季付送带宽美国洛杉矶C3机房套餐处理器内存硬盘IP数带宽线路防御价格/月套...

星梦云60元夏日促销,四川100G高防4H4G10M,西南高防月付特价

星梦云怎么样?星梦云好不好,资质齐全,IDC/ISP均有,从星梦云这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,星梦云专注四川高防服务器,成都服务器,雅安服务器 。官方网站:点击访问星梦云官网活动方案:1、成都电信年中活动机(封锁UDP,不可解封):机房CPU内存硬盘带宽IP防护流量原价活动价开通方式成都电信优化线路4vCPU4G40G+50...

gridview分页为你推荐
真正免费的网络电话谁有真正免费的网络电话??缓冲区溢出教程如何防止高手使用缓冲区溢出?镜像文件是什么什么是文件镜像?什么是镜像文件?办公协同软件最好用的协同办公软件是哪个中小企业信息化什么是中小企业信息化途径直播加速有没有软件使已经下载好了的视频播放加速,例如30分钟的视频15分钟或者20分钟播放完奇虎论坛奇虎是中国的吗?机械键盘轴打游戏用机械键盘到底什么轴好?电子商务网站模板网页制作模板虚拟机软件下载求一个免费虚拟机软件!!!请发送下载网站给我
域名注册使用godaddy 天津服务器租赁 域名备案只选云聚达 国外免费域名网站 独享100m lunarpages 主机点评 163网 gitcafe debian6 国外网站代理服务器 e蜗 日本bb瘦 web服务器的架设 idc是什么 如何用qq邮箱发邮件 空间技术网 100mbps 电信主机 银盘服务是什么 更多