我实现了datagrid的个性化分页,现在把代码贴出来朋友们参考,同时在这个功能的实现过程,参考了“飞刀”从国外翻译的一篇文章。
先定义aspx页面,注意allowcustompaging要设为"false":
<body>
<form id="dictlist" method="post" runat="server">
<table cellspacing="0" width="100%" border="1">
<tr>
<td bgcolor="#c0c000">信息:<font face="arial" color="#ffffff">数据维护</font>
</td>
</tr>
<tr>
<td><font face="宋体"></font></td>
</tr>
<tr>
<td><asp:datagrid id="mydatagrid" runat="server" width="100%" pagesize="20" allowpaging="true" autogeneratecolumns="false" datakeyfield="fdictid">
<selecteditemstyle backcolor="#ffc080"></selecteditemstyle>
<headerstyle backcolor="#c0c000"></headerstyle>
<columns>
<asp:buttoncolumn text="选择" headertext="选择" commandname="select">
<headerstyle font-bold="true" horizontalalign="center" width="8%"></headerstyle>
<itemstyle font-bold="true" horizontalalign="center"></itemstyle>
</asp:buttoncolumn>
<asp:boundcolumn datafield="fdictid" sortexpression="fdictid asc" headertext="标识号">
<headerstyle width="15%"></headerstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="fnamecn" sortexpression="fnamecn asc" headertext="名称">
<headerstyle width="15%"></headerstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="fnameen" sortexpression="fnameen asc" headertext="英文名称">
<headerstyle width="15%"></headerstyle>
</asp:boundcolumn>
<asp:boundcolumn datafield="fnote" sortexpression="fnote asc" headertext="描叙">
<headerstyle width="47%"></headerstyle>
</asp:boundcolumn>
</columns>
<pagerstyle visible="false"></pagerstyle>
</asp:datagrid></td>
</tr>
</table>
<table cellspacing="0" width="100%" bgcolor="#ff9966" border="1">
<tr>
<td align="right"><asp:linkbutton id="btnfirst" runat="server" commandargument="fist">首页</asp:linkbutton>
<asp:linkbutton id="btnprev" runat="server" width="36px" commandargument="prev">上一页</asp:linkbutton>
<asp:linkbutton id="btnnext" runat="server" commandargument="next">下一页</asp:linkbutton>
<asp:linkbutton id="btnlast" runat="server" commandargument="last">末页</asp:linkbutton>
<asp:label id="lblcurrentindex" runat="server"></asp:label>/<asp:label id="lblpagecount" runat="server"></asp:label>
跳转到<asp:textbox id="txtgopage" runat="server" width="30px" cssclass="textbox"></asp:textbox>
<asp:button id="btngo" runat="server" text="go" cssclass="button" width="29px"></asp:button></td>
</tr>
</table>
</form>
codebehind主要功能部分代码:
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
myconnection = new sqlconnection(system.configuration.configurationsettings.appsettings["connstring"]);
if (!ispostback)
bindgrid();
}
public void bindgrid()
{
string strsql ="select * from t_dict ";
sqldataadapter mycommand = new sqldataadapter(strsql, myconnection);
dataset ds = new dataset();
mycommand.fill(ds, "t_dict");
mydatagrid.datasource=ds.tables["t_dict"].defaultview;
mydatagrid.databind();
showstatspage();
}
private void pagerbuttonclick(object sender, system.eventargs e)
{
//获得linkbutton的参数值
string arg = ((linkbutton)sender).commandargument;
switch(arg)
{
case ("next"):
if (mydatagrid.currentpageindex < (mydatagrid.pagecount - 1))
mydatagrid.currentpageindex ++;
break;
case ("prev"):
if (mydatagrid.currentpageindex > 0)
mydatagrid.currentpageindex --;
break;
case ("fist"):
mydatagrid.currentpageindex=0;
break;
case ("last"):
mydatagrid.currentpageindex = (mydatagrid.pagecount - 1);
break;
default:
//本页值
mydatagrid.currentpageindex = convert.toint32(arg);
break;
}
bindgrid();
}
void showstatspage()
{
//显示页面信息
lblcurrentindex.text = "[<font color="blue">当前为第:" + ((int)mydatagrid.currentpageindex+1) + "页</font>]";
lblpagecount.text = "[<font color="blue">共:" + mydatagrid.pagecount + "页</font>]";
}
private void mydatagrid_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
//处理按下数字的方法
mydatagrid.currentpageindex = e.newpageindex;
bindgrid();
}
private void btngo_click(object sender, system.eventargs e)
{
//页面直接跳转的代码
if(txtgopage.text.trim()!="")
{
int pagei=int32.parse(txtgopage.text.trim())-1;
if (pagei >=0 && pagei < (mydatagrid.pagecount))
mydatagrid.currentpageindex = pagei ;
}
bindgrid();
}
//----------------------翻页代码结束
新闻热点
疑难解答
图片精选