GridView分页系列精装版.docx

上传人:b****1 文档编号:3478069 上传时间:2023-05-05 格式:DOCX 页数:29 大小:25KB
下载 相关 举报
GridView分页系列精装版.docx_第1页
第1页 / 共29页
GridView分页系列精装版.docx_第2页
第2页 / 共29页
GridView分页系列精装版.docx_第3页
第3页 / 共29页
GridView分页系列精装版.docx_第4页
第4页 / 共29页
GridView分页系列精装版.docx_第5页
第5页 / 共29页
GridView分页系列精装版.docx_第6页
第6页 / 共29页
GridView分页系列精装版.docx_第7页
第7页 / 共29页
GridView分页系列精装版.docx_第8页
第8页 / 共29页
GridView分页系列精装版.docx_第9页
第9页 / 共29页
GridView分页系列精装版.docx_第10页
第10页 / 共29页
GridView分页系列精装版.docx_第11页
第11页 / 共29页
GridView分页系列精装版.docx_第12页
第12页 / 共29页
GridView分页系列精装版.docx_第13页
第13页 / 共29页
GridView分页系列精装版.docx_第14页
第14页 / 共29页
GridView分页系列精装版.docx_第15页
第15页 / 共29页
GridView分页系列精装版.docx_第16页
第16页 / 共29页
GridView分页系列精装版.docx_第17页
第17页 / 共29页
GridView分页系列精装版.docx_第18页
第18页 / 共29页
GridView分页系列精装版.docx_第19页
第19页 / 共29页
GridView分页系列精装版.docx_第20页
第20页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

GridView分页系列精装版.docx

《GridView分页系列精装版.docx》由会员分享,可在线阅读,更多相关《GridView分页系列精装版.docx(29页珍藏版)》请在冰点文库上搜索。

GridView分页系列精装版.docx

GridView分页系列精装版

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"

后台方法绑定:

protectedvoidGvShow_PageIndexChanging(objectsender,GridViewPageEventArgse)

{

this.GvShow.PageIndex=e.NewPageIndex;

BindView();

}

2:

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

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

<%--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;

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

3:

分页模板中自定义分页的样式,虽然微软开辟了这个模板提供给用户类似于自定义分页的功能,但这个功能完全还是基于微软的GridView自带的分页进行的,属性的Visable的属性必须是trueAllowPaging="true"与PageSize="3"属性页都要进行相关的有效设置才可以。

这种情况下的分页可以不使用onpageindexchanging="GvShow_PageIndexChanging"微软自带的分页事件,开发自己独立的分页事件与方法即可

范例:

前台代码:

GridViewid="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">

BackColor="#BFD5FA">

<%----%>

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

TemplateFieldHeaderText="省份">

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

Label>

TemplateField>

TemplateFieldHeaderText="城市">

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

Label>

TemplateField>

TemplateFieldHeaderText="用户名">

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

Label>

TemplateField>

linkbuttonid="cmdbegin"runat="server"CommandName="begin">首页

linkbutton>』

linkbuttonid="cmdbefore"runat="server"CommandName="before">上一页

linkbutton>』

linkbuttonid="cmdafter"runat="server"CommandName="after">下一页

linkbutton>』

linkbuttonid="cmdend"runat="server"CommandName="end">尾页

linkbutton>』

页次:

labelid="txtNowPage"runat="server"ForeColor="red">0

label>/

labelid="txtAllPage"runat="server"ForeColor="red">0

label>页 

labelid="txtTotal"runat="server"ForeColor="red">0

label>条记录 

labelid="txtNowRed"runat="server"ForeColor="red">0

label>条记录/页

checkboxid="cmdCheck"runat="server"Text="显示数字按钮"AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged">

checkbox>

GridView>

后台代码:

namespaceGridViewDemo.GridView分页系列

{

publicpartialclassGridView自定义分页02:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

BindView();

InitButtons();

}

}

privatevoidBindView()

{

DataTabledt=UserDemoAccess.GetUserSouce();

ViewState["RowCounts"]=dt.Rows.Count.ToString();

this.GvShow.DataSource=dt;;

this.GvShow.DataBind();

}

protectedvoidGvShow_PageIndexChanging(objectsender,GridViewPageEventArgse)

{

this.GvShow.PageIndex=e.NewPageIndex;

BindView();

InitButtons();

}

protectedvoidGvShow_RowDataBound(objectsender,GridViewRowEventArgse)

{

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)

//{

//GridViewRowgr=e.Row;

////页次--第几页

//LabeltxtNowPage=gr.FindControl("txtNowPage")asLabel;

////总页数

//LabeltxtAllPage=gr.FindControl("txtAllPage")asLabel;

////总记录数

//LabeltxtTotal=gr.FindControl("txtTotal")asLabel;

////条记录/页

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

//}

}

protectedvoidcmdCheck_CheckedChanged(objectsender,EventArgse)

{

//CheckBoxcmdCheck=this.GvShow.BottomPagerRow.FindControl("cmdCheck")asCheckBox;

//if(cmdCheck.Checked)

//{

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

//}

}

protectedvoidGvShow_RowCommand(objectsender,GridViewCommandEventArgse)

{

//控制页签

switch(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.PageIndex+=1;

}

;break;

case"end":

this.GvShow.PageIndex=this.GvShow.PageCount-1;

;break;

}

//控制按钮

InitButtons();

}

 

privatevoidInitButtons()

{

//获取gridviewrows的PagerTemplate底部模板

GridViewRowgr=this.GvShow.BottomPagerRow;

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

LinkButtoncmdbefore=gr.FindControl("cmdbefore")asLinkButton;

LinkButtoncmdafter=gr.FindControl("cmdafter")asLinkButton;

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

 

//页次--第几页

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

//总页数

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

//总记录数

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

//条记录/页

LabeltxtNowRed=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)

{

cmdbegin.Enabled=false;

cmdbefore.Enabled=false;

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

{

cmdafter.Enabled=false;

cmdend.Enabled=false;

}

}

}

}

}

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

前台代码:

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

linkbutton>』

linkbuttonid="cmdbefore"runat="server"CommandName="before"OnCommand="PagerButton_Command">上一页

linkbutton>』

linkbuttonid="cmdafter"runat="server"CommandName="after"OnCommand="PagerButton_Command">下一页

linkbutton>』

linkbuttonid="cmdend"runat="server"CommandName="end"OnCommand="PagerButton_Command">尾页

linkbutton>』

页次:

labelid="txtNowPage"runat="server"ForeColor="red">0

label>/

labelid="txtAllPage"runat="server"ForeColor="red">0

label>页 

labelid="txtTotal"runat="server"ForeColor="red">0

label>条记录 

labelid="txtNowRed"runat="server"ForeColor="red">0

label>条记录/页

checkboxid="cmdCheck"runat="server"Text="显示数字按钮"AutoPostBack="True"oncheckedchanged="cmdCheck_CheckedChanged">

checkbox>

这俩种方式的区别在于:

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

后台代码:

protectedvoidPagerButton_Command(ob

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2