.net GridView分页模板的实例代码
//要在gridview中加入如下属性(前台还是后台看你的习惯了。)
//实现分页
allowpaging="true"
//一页数据10行
pagesize="10"
// 分页时触发的事件
onpageindexchanging="gvwdesignationname_pageindexchanging"
//在服务器事件里
protected void gvwdesignationname_pageindexchanging(object sender, gridviewpageeventargs e)
{
gvwdesignationname.pageindex=e.newindex;
bingdesignatioonname();
}
//这里我给出一个通用显示分页的模板
<pagertemplate>
当前第:
//((gridview)container.namingcontainer)就是为了得到当前的控件
<asp:label id="labelcurrentpage" runat="server" text="<%# ((gridview)container.namingcontainer).pageindex + 1 %>"></asp:label>
页/共:
//得到分页页面的总数
<asp:label id="labelpagecount" runat="server" text="<%# ((gridview)container.namingcontainer).pagecount %>"></asp:label>
页
//如果该分页是首分页,那么该连接就不会显示了.同时对应了自带识别的命令参数commandargument
<asp:linkbutton id="linkbuttonfirstpage" runat="server" commandargument="first" commandname="page"
visible='<%#((gridview)container.namingcontainer).pageindex != 0 %>'>首页</asp:linkbutton>
<asp:linkbutton id="linkbuttonpreviouspage" runat="server" commandargument="prev"
commandname="page" visible='<%# ((gridview)container.namingcontainer).pageindex != 0 %>'>上一页</asp:linkbutton>
//如果该分页是尾页,那么该连接就不会显示了
<asp:linkbutton id="linkbuttonnextpage" runat="server" commandargument="next" commandname="page"
visible='<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>'>下一页</asp:linkbutton>
<asp:linkbutton id="linkbuttonlastpage" runat="server" commandargument="last" commandname="page"
visible='<%# ((gridview)container.namingcontainer).pageindex != ((gridview)container.namingcontainer).pagecount - 1 %>'>尾页</asp:linkbutton>
转到第
<asp:textbox id="txtnewpageindex" runat="server" width="20px" text='<%# ((gridview)container.parent.parent).pageindex + 1 %>' />页
//这里将commandargument即使点击该按钮e.newindex 值为3
<asp:linkbutton id="btngo" runat="server" causesvalidation="false" commandargument="-2"
commandname="page" text="go" />
</pagertemplate>
//对应该事件中代码为
protected void gvwdesignationname_pageindexchanging(object sender, gridviewpageeventargs e)
{
// 得到该控件
gridview thegrid = sender as gridview;
int newpageindex = 0;
if (e.newpageindex==-3)
{
//点击了go按钮
textbox txtnewpageindex = null;
//gridview较datagrid提供了更多的api,获取分页块可以使用bottompagerrow 或者toppagerrow,当然还增加了headerrow和footerrow
gridviewrow pagerrow = thegrid.bottompagerrow;
if (pagerrow != null)
{
//得到text控件
txtnewpageindex = pagerrow.findcontrol("txtnewpageindex") as textbox;
}
if ( txtnewpageindex!= null)
{
//得到索引
newpageindex = int.parse(txtnewpageindex.text) - 1;
}
}
else
{
//点击了其他的按钮
newpageindex = e.newpageindex;
}
//防止新索引溢出
newpageindex = newpageindex < 0 ? 0 : newpageindex;
newpageindex = newpageindex >= thegrid.pagecount ? thegrid.pagecount - 1 : newpageindex;
//得到新的值
thegrid.pageindex = newpageindex;
//重新绑定
bingdesignatioonname();
}