GridView分页系列

GridView分页系列


2024年2月9日发(作者:)

GridView分页系列

1:GridView自带分页:GridView自带的分页,是假分页,他每次从数据库把数据全部查询出之后,通过分页的算法,进行按每页数量进行分页。

分页的属性元素:分页功能的实现就是通过对这些属性元素的操作实现的。

//dex 当前页的索引

//unt 总共的页数

// 当前页签内的gridview的行数

//ze 每页的记录数

//dex* + 1 行索引

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

后台方法绑定:

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

dex = eIndex;

BindView();

}

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

普通的GridView自带的分页,不带样式,只是普通的1,2,3等,如果希望获取到具有其他分页样式就应该设置属性

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

后台访问此属性的实例:

ageText = "首页";

geText = "尾页";

geText = "下一页";

usPageText = "上一页";

= eviousFirstLast;

通过属性可以设置GRIDVIEW分页的样式

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

范例:

前台代码:

BorderWidth="1px" PageSize="3"

1

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

AutoGenerateColumns="False" onpageindexchanging="GvShow_PageIndexChanging"

AllowPaging="true"

onrowdatabound="GvShow_RowDataBound" Height="132px"

onrowcommand="GvShow_RowCommand">

BackColor="#FFFF99">

BackColor="#BFD5FA">

ForeColor="Black" VerticalAlign="Middle"

<%----%>

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

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

VerticalAlign="Middle">

Text='<%#Eval("rname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("cname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("username") %>'>

2

align="center" border="0">

id="cmdbegin" runat="server" CommandName="begin" >首页』

CommandName="before" >上一页』

CommandName="after" >下一页』

CommandName="end" >尾页』

页次:

id="txtNowPage" runat="server" ForeColor="red">0/

runat="server" ForeColor="red">0页 

ForeColor="red">0条记录 

ForeColor="red">0条记录/页

id="cmdCheck" runat="server" Text="显示数字按钮" AutoPostBack="True"

oncheckedchanged="cmdCheck_CheckedChanged" >

后台代码:

namespace ew分页系列

{

public partial class GridView自定义分页02 :

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindView();

InitButtons();

}

}

private void BindView()

{

3

DataTable dt = rSouce();

ViewState["RowCounts"] = ng();

urce = dt;;

nd();

}

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

dex = eIndex;

BindView();

InitButtons();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e == w)

{

("onmouseover",

"ribute('BKC',oundColor);='default',oundColor='#ffff99'");

("onmouseout",

"oundColor=ribute('BKC');");

}

//if(e == )

//{

// GridViewRow gr = ;

// //页次 -- 第几页

// Label txtNowPage = ntrol("txtNowPage") as Label;

// //总页数

// Label txtAllPage = ntrol("txtAllPage") as Label;

// //总记录数

// Label txtTotal = ntrol("txtTotal") as Label;

// //条记录/页

// Label txtNowRed = ntrol("txtNowRed") as Label;

// = (dex+1).ToString();

// = ng();

// = ViewState["RowCounts"].ToString();

// = ng();

4

//}

}

protected void cmdCheck_CheckedChanged(object sender, EventArgs e)

{

//CheckBox cmdCheck = ntrol("cmdCheck") as

CheckBox;

//if (d)

//{

// = c;

//}

}

protected void GvShow_RowCommand(object sender, GridViewCommandEventArgs e)

{

//控制页签

switch(dName)

{

case "begin":

dex = 0;

; break;

case "before":

if(dex > 0)

{

dex -= 1;

}

; break;

case "after":

if(dex < unt - 1)

{

dex += 1;

}

; break;

case "end":

dex = unt-1;

; break;

}

//控制按钮

InitButtons();

}

5

private void InitButtons()

{

//获取gridviewrows的PagerTemplate底部模板

GridViewRow gr = PagerRow;

LinkButton cmdbegin = ntrol("cmdbegin") as LinkButton;

LinkButton cmdbefore = ntrol("cmdbefore") as LinkButton;

LinkButton cmdafter = ntrol("cmdafter") as LinkButton;

LinkButton cmdend = ntrol("cmdend") as LinkButton;

//页次 -- 第几页

Label txtNowPage = ntrol("txtNowPage") as Label;

//总页数

Label txtAllPage = ntrol("txtAllPage") as Label;

//总记录数

Label txtTotal = ntrol("txtTotal") as Label;

//条记录/页

Label txtNowRed = ntrol("txtNowRed") as Label;

= (dex + 1).ToString();

= ng();

= ViewState["RowCounts"].ToString();

= ng();

d = false;

d = false;

d = false;

d = false;

if(unt > 1)

{

d = true;

d = true;

d = true;

d = true;

if(dex == 0)

{

d = false;

d = false;

6

}else if(dex == unt-1)

{

d = false;

d = false;

}

}

}

}

}

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

前台代码:

align="center" border="0">

id="cmdbegin" runat="server" CommandName="begin" OnCommand="PagerButton_Command" >首页』

CommandName="before" OnCommand="PagerButton_Command" >上一页』

CommandName="after" OnCommand="PagerButton_Command" >下一页』

CommandName="end" OnCommand="PagerButton_Command" >尾页』

页次:

id="txtNowPage" runat="server" ForeColor="red">0/

runat="server" ForeColor="red">0页 

ForeColor="red">0条记录 

ForeColor="red">0条记录/页

id="cmdCheck" runat="server" Text="显示数字按钮" AutoPostBack="True"

oncheckedchanged="cmdCheck_CheckedChanged" >

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

7

后台代码:

protected void PagerButton_Command(object sender, dEventArgs

e)

{

switch (dName)

{

case "begin":

dex = 0;

; break;

case "before":

if (dex > 0)

{

dex -= 1;

}

; break;

case "after":

if (dex < unt - 1)

{

dex += 1;

}

; break;

case "end":

dex = unt - 1;

; break;

}

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

BindView();

InitButtons();

}

一个很好样式的假分页,功能齐全:

前台页面:

BorderColor="#687BC4"

BorderWidth="1px" PageSize="3"

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

AutoGenerateColumns="False"

AllowPaging="true"

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

BackColor="#FFFF99">

8

BackColor="#BFD5FA">

ForeColor="Black" VerticalAlign="Middle"

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

Visible="false" />

VerticalAlign="Middle">

Text='<%#Eval("rname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("cname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("username") %>'>

width="100%" align="center" border="0">

9

id="cmdbegin" runat="server" CommandName="begin" OnCommand="PagerButton_Command" >首页』

CommandName="before" OnCommand="PagerButton_Command" >上一页』

CommandName="after" OnCommand="PagerButton_Command" >下一页』

CommandName="end" OnCommand="PagerButton_Command" >尾页』

页次:

id="txtNowPage" runat="server" ForeColor="red">0/

runat="server" ForeColor="red">0页 

ForeColor="red">0条记录 

ForeColor="red">0条记录/页

runat="server" Text="跳转"

onclick="lbtnTurnNewPage_Click">

Width="20" Height="20">

runat="server" AutoPostBack="true"

onselectedindexchanged="drpPageNumbers_SelectedIndexChanged">

后台页面:

namespace ew分页系列

{

public partial class GridView自定义分页04 :

{

protected void Page_Load(object sender, EventArgs e)

10

{

if (!IsPostBack)

{

BindView();

InitButtons();

InitDrp();

}

}

private void BindView()

{

DataTable dt = rSouce();

ViewState["PageCount"] = ng();

urce = dt; ;

nd();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e == w)

{

("onmouseover",

"ribute('BKC',oundColor);='default',oundColor='#ffff99'");

("onmouseout",

"oundColor=ribute('BKC');");

}

}

protected void PagerButton_Command(object sender,

dEventArgs e)

{

switch (dName)

{

case "begin":

dex = 0;

; break;

case "before":

if (dex > 0)

{

dex -= 1;

}

; break;

case "after":

11

if (dex < unt - 1)

{

dex += 1;

}

; break;

case "end":

dex = unt - 1;

; break;

}

BindView();

InitButtons();

}

private void InitButtons()

{

= (dex + 1).ToString();

= ng();

= ViewState["PageCount"].ToString();

= ng();

d = false;

d = false;

d = false;

d = false;

if (unt > 1)

{

d = true;

d = true;

d = true;

d = true;

if (dex == 0)

{

d = false;

d = false;

}

else if (dex == unt - 1)

{

d = false;

d = false;

}

}

12

}

protected void lbtnTurnNewPage_Click(object sender, EventArgs e)

{

try

{

int pageIndexNumber = 32(());

if (pageIndexNumber > unt)

{

("越界!");

return;

}

else if (pageIndexNumber <= 0)

{

("负数不灵!");

return;

}

dex = pageIndexNumber-1;

BindView();

InitButtons();

}

catch

{

("请输入数字!");

return;

}

}

#region 下拉列表分页跳转

protected void drpPageNumbers_SelectedIndexChanged(object sender, EventArgs e)

{

DropDownList drpPageNumbers = (DropDownList)sender;

//改变GridView的页签

dex = 32(ControlDrp);

//页签改变后,重新绑定

BindView();

InitButtons();

}

private void InitDrp()

{

();

for (int i = 0; i

{

13

(new ListItem((i+1).ToString(), ng()));

}

}

private String ControlDrp

{

set

{

edValue = value;

}

get

{

return edValue;

}

}

#endregion

}

}

一个利用 模板实现的假分页实例:(可以在绑定的时候加入dropdownlist的页面选择)

前台页面:

BorderWidth="1px" PageSize="3"

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

AutoGenerateColumns="False"

AllowPaging="true"

onrowdatabound="GvShow_RowDataBound" Height="132px"

onpageindexchanging="GvShow_PageIndexChanging" >

BackColor="#BFD5FA">

BackColor="#FFFF99">

ForeColor="Black" VerticalAlign="Middle"

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

VerticalAlign="Middle">

14

Text='<%#Eval("rname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("cname") %>' >

VerticalAlign="Middle">

Text='<%#Eval("username") %>'>

[第

Text="<%#((GridView)).PageIndex + 1 %>">页]

[共

Text="<%# ((GridView)).PageCount %>">页]

CausesValidation="False" CommandArgument="First"

CommandName="Page" Enabled="<%#

((GridView)Container).PageIndex != 0 %>"

Text="首页">

CausesValidation="False" CommandArgument="Prev"

CommandName="Page" Enabled=" <%#

((GridView)Container).PageIndex != 0 %>"

Text="上一页">

CausesValidation="False" CommandArgument="Next"

CommandName="Page" Enabled=" <%#

((GridView)Container).PageIndex !=

((GridView)Container).PageCount - 1 %>"

Text="下一页">

15

CausesValidation="False" CommandArgument="Last"

CommandName="Page" Enabled=" <%#

((GridView)Container).PageIndex !=

((GridView)Container).PageCount - 1 %>"

Text="尾页">

Text="<%# ((GridView)).PageIndex + 1%>"

Width="20px">

CausesValidation="false" CommandArgument="-1"

CommandName="Page" Text="GO" >

namespace ew分页系列

{

public partial class GridView自定义分页05 :

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindView();

}

}

private void BindView()

{

DataTable dt = rSouce();

urce = dt; ;

nd();

}

protected void GvShow_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e == w)

{

("onmouseover",

"ribute('BKC',oundColor);='default',.

16

backgroundColor='#ffff99'");

("onmouseout",

"oundColor=ribute('BKC');");

}

}

protected void GvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridViewRow gr = PagerRow;

TextBox txtNewPageIndex = ntrol("txtNewPageIndex") as TextBox;

string content = CheckPageIndex();

if (content != )

{

Show(this, content);

//如果发现异常,则重新将文本框中的值,置换成当前页签的!

= (dex+1).ToString();

return;

}//NewPageIndex 本页跳转点击go的时候,为-2,增加一个处理!微软自带分页中,选中页签当前页无法再点击

//此时的eIndex默认为-2

else if (eIndex < 0)

{

int pageIndexTemp = 32() - 1;

dex = pageIndexTemp;

BindView();

return;

}

dex = eIndex;

BindView();

}

//protected void LinkTurn_Command(object

sender,dEventArgs e)

//{

// if (ng() == "-1")

// {

// GridViewRow gr = PagerRow;

// TextBox txtNewPageIndex = ntrol("txtNewPageIndex") as TextBox;

// try

// {

17

// int pageIndex = 32();

// string content = CheckPageIndex(pageIndex, "command");

// if ( content != )

// {

// Show(this, content);

// return;

// }

// dex = pageIndex - 1;

// BindView();

// }

// catch

// {

// Show(this, "只能输入数字格式!");

// return;

// }

// }

//}

private string CheckPageIndex()

{

string cotent = ;

GridViewRow gr = PagerRow;

TextBox txtNewPageIndex = ntrol("txtNewPageIndex") as TextBox;

try

{

int pageIndexTemp = 32();

if (pageIndexTemp <= 0 || pageIndexTemp > unt)

{

cotent = "你越位了,朋友!";

}

}

catch

{

cotent = "请输入数字!";

}

return cotent;

}

public static void Show( page, string msg)

{

erStartupScript(e(), "message", "");

}

}

}

19


发布者:admin,转转请注明出处:http://www.yc00.com/web/1707412167a1504448.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信