zoukankan
html css js c++ java
gridview自定义分页
gridview里虽然有自动分页的功能,但是我们其实经常感觉不是很完善,比如,他没有页面直接跳转的功能,就是输入页数,然后跳转。还有他也没有显示记录的条数。其实,这些信息有时候是非常重要的。因此我们非常有必要实现这个功能。下面,我就讲述下如何实现自定义分页的。
一、首先要将gridview设置成可以分页,然后我们在增加如下代码:
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
Width
=
"
98%
"
AllowPaging
=
"
True
"
OnPageIndexChanging
=
"
GridView1_PageIndexChanging
"
PageSize
=
"
20
"
>
<
Columns
>
<
asp:BoundField DataField
=
"
UserName
"
HeaderText
=
"
登陆用户名
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
Name
"
HeaderText
=
"
真实姓名
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
PostName
"
HeaderText
=
"
职位
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
OrgName
"
HeaderText
=
"
组织
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
RoleName
"
HeaderText
=
"
角色
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
StateStr
"
HeaderText
=
"
当前状态
"
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:BoundField
>
<
asp:TemplateField HeaderText
=
"
操作
"
>
<
ItemTemplate
>
<
asp:LinkButton ID
=
"
LinkButtonEdit
"
runat
=
"
server
"
CommandArgument
=
'
<%# Eval("UserID") %>
'
OnClick
=
"
LinkButtonEdit_Click
"
>
编辑
</
asp:LinkButton
>
&
nbsp;
<
asp:LinkButton ID
=
"
LinkButtonDelete
"
runat
=
"
server
"
CommandArgument
=
'
<%# Eval("UserID") %>
'
OnClick
=
"
LinkButtonDelete_Click
"
>
删除
</
asp:LinkButton
>
</
ItemTemplate
>
<
ItemStyle CssClass
=
"
griditem
"
/>
<
HeaderStyle CssClass
=
"
gridhead
"
/>
</
asp:TemplateField
>
</
Columns
>
<
EmptyDataTemplate
>
<
table border
=
'
0
'
cellpadding
=
'
0
'
cellspacing
=
'
0
'
style
=
'
border-right: coral 1px dotted;border-top: coral 1px dotted; border-left: coral 1px dotted; border-bottom: coral 1px dotted;background-color: #ffffff;200px;height:55px;
'
><
tr
><
td align
=
'
center
'
style
=
"
height: 53px
"
><
font color
=
'
coral
'
><
b
>
没有任何数据
<
b
></
font
></
B
></
B
></
td
></
tr
></
table
>
</
EmptyDataTemplate
>
<PagerTemplate>
<table border="0" cellpadding="0" cellspacing="0" style=" 99%; height: 15px">
<tr>
<td style=" 100px; height: 13px">
</td>
<td style=" 100px; height: 13px">
<div style=" 286px; height: 7px; font-size:12px;">
总共
<%=RecordCount%>
条记录 当前第
<%#((GridView)Container.NamingContainer).PageIndex+1%>/<%#((GridView)Container.NamingContainer).PageCount %>
页
</div>
</td>
<td style=" 100px; height: 13px">
<div style=" 252px; height: 8px; text-align: right">
<asp:LinkButton ID="FirstPage" runat="server" CommandArgument="First" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">第一页</asp:LinkButton>
<asp:LinkButton ID="PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="NextPage" runat="server" CommandArgument="Next" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LastPage" runat="server" CommandArgument="Last" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">最后一页</asp:LinkButton>
<asp:TextBox ID="TextGotoPage" runat="server" CssClass="pageeditbox"></asp:TextBox>
<asp:Button ID="PageGo" causesvalidation="False" commandargument="-1" commandname="Page" runat="server" CssClass="pagebutton" Text="Go" />
</div>
</td>
<td style=" 100px; height: 13px">
<div style=" 266px; height: 1px">
</div>
</td>
</tr>
</table>
</PagerTemplate>
</
asp:GridView
>
红色的部分就是自定义的分页代码,其实LinkButton里的
CommandArgument
参数就是gridview的当前页,
CommandName
表示触发的命令,这些都不能改。
注意里面有个RecordCount的变量,他是public类型,是从后台传过来的,表示记录的条数,测试的时候别忘了这个变量啊。
二、分页的代码
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
SetGridPage(sender, e,
DtSource)
}
SetGridPage的函数
/**/
///
<summary>
///
分页处理
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
///
<param name="dt"></param>
public
void
SetGridPage(
object
sender, GridViewPageEventArgs e,DataTable dt)
{
GridView theGrid
=
(GridView)sender;
//
refer to the GridView
int
newPageIndex
=
0
;
GridViewRow pagerRow
=
theGrid.BottomPagerRow;
//
GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow;
//
refer to PagerTemplate
//
GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
if
(
-
2
==
e.NewPageIndex)
//
点击按钮的事件
{
TextBox txtNewPageIndex
=
null
;
if
(
null
!=
pagerRow)
{
txtNewPageIndex
=
(TextBox)pagerRow.FindControl(
"
TextGotoPage
"
);
//
refer to the TextBox with the NewPageIndex value
}
if
(
null
!=
txtNewPageIndex
&&
txtNewPageIndex .Text
!=
""
)
{
newPageIndex
=
int
.Parse(txtNewPageIndex.Text)
-
1
;
//
get the NewPageIndex
}
else
{
newPageIndex
=
0
;
}
}
else
{
//
当点击分页连接的时候
newPageIndex
=
e.NewPageIndex;
}
//
处理超出范围的分页
newPageIndex
=
newPageIndex
<
0
?
0
: newPageIndex;
newPageIndex
=
newPageIndex
>=
theGrid.PageCount
?
theGrid.PageCount
-
1
: newPageIndex;
((TextBox)pagerRow.FindControl(
"
TextGotoPage
"
)).Text
=
Convert.ToString(newPageIndex
+
1
);
//
帮定数据源
theGrid.PageIndex
=
newPageIndex;
theGrid.DataSource
=
dt;
theGrid.DataBind();
}
查看全文
相关阅读:
Codeforces Round #344 (Div. 2) C. Report 其他
Codeforces Round #344 (Div. 2) B. Print Check 水题
Codeforces Round #344 (Div. 2) A. Interview 水题
8VC Venture Cup 2016
CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂 中二版
CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂
CDOJ 1279 班委选举 每周一题 div2 暴力
每周算法讲堂 快速幂
8VC Venture Cup 2016
Educational Codeforces Round 9 F. Magic Matrix 最小生成树
原文地址:https://www.cnblogs.com/ringwang/p/992062.html
最新文章
Android编程之仿微信显示更多文字的View
理解MapReduce哲学
mysql 备份
java学习路线(好资源大家分享)
android 根据SD卡中图片路径读取并显示SD中的图片——源代码
Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
Codeforces Round #345 (Div. 2) E. Table Compression 并查集
Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
Codeforces Round #345 (Div. 1) A
热门文章
Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力
Codeforces Round #345 (Div. 2) A. Joysticks dp
Codeforces Beta Round #1 B. Spreadsheets 模拟
HDU 5639 Deletion 二分+网络流
HDU 5638 Toposort 拓扑排序 优先队列
HDU 5637 Transform 最短路
HDU 5636 Shortest Path 暴力
51nod 1640 天气晴朗的魔法 最小生成树
Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
Codeforces Round #344 (Div. 2) D. Messenger kmp
Copyright © 2011-2022 走看看