Asp.net中使用PageDataSource分页实现代码
注:封装数据绑定控件(如 system.web.ui.webcontrols.datagrid、system.web.ui.webcontrols.gridview、system.web.ui.webcontrols.detailsview
// 和 system.web.ui.webcontrols.formview)的与分页相关的属性,以允许该控件执行分页操作。无法继承此类。
datalist
public datatable getdataset(string sql)
{
sqlconnection conn = this.getconn();
sqldataadapter sdr = new sqldataadapter(sql, conn);
dataset rs = new dataset();
sdr.fill(rs);
return rs.tables[0];
}
public pageddatasource pagedatalistbind(string sql, int currentpage, int pagesize)
{
pageddatasource pds = new pageddatasource();
pds.datasource = getdataset(sql).defaultview;
pds.allowpaging = true;
pds.pagesize = pagesize;
pds.currentpageindex = currentpage - 1;
return pds;
}
db db = new db();
pageddatasource pds = new pageddatasource();
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind();
}
}
public void bind()
{
pds = db.pagedatalistbind("select * from tb_word",convert.toint32(lblcurrentpage.text),2);
lnkbtnfirst.enabled = true;
lnkbtnlast.enabled = true;
lnkbtnprevious.enabled = true;
lnkbtnnext.enabled = true;
if (lblcurrentpage.text == "1")
{
lnkbtnfirst.enabled = false;
lnkbtnprevious.enabled = false;
}
if(lblcurrentpage.text==pds.pagecount.tostring())
{
lnkbtnlast.enabled = false;
lnkbtnnext.enabled = false;
}
lblsumpage.text = pds.pagecount.tostring();
datalist1.datasource = pds;
datalist1.datakeyfield = "id";
datalist1.databind();
}
protected void lnkbtnfirst_click(object sender, eventargs e)
{
lblcurrentpage.text = "1";
bind();
}
protected void lnkbtnprevious_click(object sender, eventargs e)
{
lblcurrentpage.text = (convert.toint32(lblcurrentpage.text) - 1).tostring();
bind();
}
protected void lnkbtnnext_click(object sender, eventargs e)
{
lblcurrentpage.text = (convert.toint32(lblcurrentpage.text) + 1).tostring();
bind();
}
protected void lnkbtnlast_click(object sender, eventargs e)
{
lblcurrentpage.text = lblsumpage.text;
bind();
}
<form id="form1" runat="server">
<div>
<asp:datalist id="datalist1" runat="server">
<itemtemplate>
<table border="1">
<tr>
<td>id</td><td>标题</td><td>内容</td>
</tr>
<tr>
<td><%#eval("id") %></td>
<td><%#eval("title") %></td>
<td><%#eval("content") %></td>
</tr>
</table>
</itemtemplate>
</asp:datalist>
当前页码为[<asp:label id="lblcurrentpage" runat="server" text="1"></asp:label>]页
总页码[<asp:label
id="lblsumpage" runat="server" text="0"></asp:label>]页
<asp:linkbutton id="lnkbtnfirst" runat="server" font-underline="false" onclick="lnkbtnfirst_click" > 第一页 </asp:linkbutton>
<asp:linkbutton id="lnkbtnprevious" runat="server" font-underline="false" onclick="lnkbtnprevious_click" > 上一页 </asp:linkbutton>
<asp:linkbutton id="lnkbtnnext" runat="server" font-underline="false" onclick="lnkbtnnext_click" > 下一页 </asp:linkbutton>
<asp:linkbutton id="lnkbtnlast" runat="server" font-underline="false" onclick="lnkbtnlast_click"> 末一页 </asp:linkbutton>
</div>
</form>
推荐阅读
-
Asp.net中使用PageDataSource分页实现代码
-
asp.net Repeater分页实例(PageDataSource的使用)
-
asp.net中使用repeater和PageDataSource搭配实现分页代码
-
使用ASP.NET MVC 4 Async Action+jQuery实现消息通知机制的实现代码
-
asp.net中使用cookie与md5加密实现记住密码功能的实现代码
-
ASP.NET中repeater嵌套实现代码(附源码)
-
asp.net中倒计时自动跳转页面的实现方法(使用javascript)
-
asp.net中gridview的查询、分页、编辑更新、删除的实例代码
-
asp.net中的“按需打印”(打印你需要打印的部分) 实现代码
-
使用asp.net的pageDataSource中时索引问题解决方法