repeater分页显示
简单的说,先从控件工具箱中拖入两个label(currten page当前页,count page总页),四个linkbuttion(首 / 下页 / 上页 / 尾页)。
无非就是 + 1 - 1 的问题,
------------------------
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
public partial class gonggao : system.web.ui.usercontrol
... {
protected void page_load(object sender, eventargs e)
...{
if (!ispostback)
...{
this.labpage.text = "1";
this.contrlrepeater();
}
}
//获取指字符个数的字符
public string cuts(string aa,int bb)
...{
if (aa.length <= bb) ...{ return aa; }
else ...{ return aa.substring(0, bb); }
}
//repeater分页控制显示方法
public void contrlrepeater()
...{
pb pb1 = new pb();
dataset ss = new dataset();
ss = pb1.returnds("select top 200 [标题],[时间] from news");
pageddatasource pds = new pageddatasource();
pds.datasource = ss.tables["temptable"].defaultview;
pds.allowpaging = true;
pds.pagesize = 2;
pds.currentpageindex = convert.toint32(this.labpage.text) - 1;
repeater1.datasource = pds;
labcountpage.text = pds.pagecount.tostring();
labpage.text = (pds.currentpageindex + 1).tostring();
this.lbtnpritpage.enabled = true;
this.lbtnfirstpage.enabled = true;
this.lbtnnextpage.enabled = true;
this.lbtndownpage.enabled = true;
if(pds.currentpageindex<1)
...{
this.lbtnpritpage.enabled = false;
this.lbtnfirstpage.enabled = false;
}
if (pds.currentpageindex == pds.pagecount-1)
...{
this.lbtnnextpage.enabled = false;
this.lbtndownpage.enabled = false;
}
repeater1.databind();
}
protected void lbtnpritpage_click(object sender, eventargs e)
...{
this.labpage.text = convert.tostring(convert.toint32(labpage.text) - 1);
this.contrlrepeater();
}
protected void lbtnfirstpage_click(object sender, eventargs e)
...{
this.labpage.text = "1";
this.contrlrepeater();
}
protected void lbtndownpage_click(object sender, eventargs e)
...{
this.labpage.text =this.labcountpage.text;
this.contrlrepeater();
}
protected void lbtnnextpage_click(object sender, eventargs e)
...{
this.labpage.text = convert.tostring(convert.toint32(labpage.text) + 1);
this.contrlrepeater();
}
}
--------------------------------------------------------
aspx文件:
<% @ control language = " c# " autoeventwireup = " true " codefile = " gonggao.ascx.cs " inherits = " gonggao " %>
< table >
< asp:repeater id = " repeater1 " runat = " server " >
< itemtemplate >
< tr >< td >< a href = "" ><% # cuts(databinder.eval(container.dataitem, " [标题] " ).tostring(), 8 ) %></ a ></ td >
< td >< a href = "" ><% # cuts(databinder.eval(container.dataitem, " [时间] " ).tostring(), 5 ) %></ a ></ td ></ tr >
</ itemtemplate >
</ asp:repeater >
</ table >
< table border = " 1 " >< tr >< td align = " center " >
< asp:linkbutton id = " lbtnfirstpage " runat = " server " onclick = " lbtnfirstpage_click " > 页首 </ asp:linkbutton >
< asp:linkbutton id = " lbtnpritpage " runat = " server " onclick = " lbtnpritpage_click " > 上一页 </ asp:linkbutton >
< asp:linkbutton id = " lbtnnextpage " runat = " server " onclick = " lbtnnextpage_click " > 下一页 </ asp:linkbutton >
< asp:linkbutton id = " lbtndownpage " runat = " server " onclick = " lbtndownpage_click " > 页尾 </ asp:linkbutton >< br />
第 < asp:label id = " labpage " runat = " server " text = " label " ></ asp:label > 页 / 共 < asp:label id = " labcountpage " runat = " server " text = " label " ></ asp:label > 页 跳至 < asp:dropdownlist
id = " dropdownlist1 " runat = " server " >
</ asp:dropdownlist >
</ td ></ tr >
</ table >
附:pb.returnds(根据sql语句返回dataset数据集.temptable表)
public dataset returnds( string sqlstr)
... {
dataset ds = new dataset();
try
...{
//conn.open();
sqlcommand comm = new sqlcommand(sqlstr, conn);
comm.commandtimeout = 20;
this.conn.open();
sqldataadapter sda = new sqldataadapter(comm);
sda.fill(ds, "temptable");
return ds;
conn.close(); conn.dispose(); comm.dispose();
}
catch (exception e)
...{
throw (e);
// ds = null;
return ds;
}
finally
...{
this.conn.close();
}
}
简单的说,先从控件工具箱中拖入两个label(currten page当前页,count page总页),四个linkbuttion(首 / 下页 / 上页 / 尾页)。
无非就是 + 1 - 1 的问题,
------------------------
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
public partial class gonggao : system.web.ui.usercontrol
... {
protected void page_load(object sender, eventargs e)
...{
if (!ispostback)
...{
this.labpage.text = "1";
this.contrlrepeater();
}
}
//获取指字符个数的字符
public string cuts(string aa,int bb)
...{
if (aa.length <= bb) ...{ return aa; }
else ...{ return aa.substring(0, bb); }
}
//repeater分页控制显示方法
public void contrlrepeater()
...{
pb pb1 = new pb();
dataset ss = new dataset();
ss = pb1.returnds("select top 200 [标题],[时间] from news");
pageddatasource pds = new pageddatasource();
pds.datasource = ss.tables["temptable"].defaultview;
pds.allowpaging = true;
pds.pagesize = 2;
pds.currentpageindex = convert.toint32(this.labpage.text) - 1;
repeater1.datasource = pds;
labcountpage.text = pds.pagecount.tostring();
labpage.text = (pds.currentpageindex + 1).tostring();
this.lbtnpritpage.enabled = true;
this.lbtnfirstpage.enabled = true;
this.lbtnnextpage.enabled = true;
this.lbtndownpage.enabled = true;
if(pds.currentpageindex<1)
...{
this.lbtnpritpage.enabled = false;
this.lbtnfirstpage.enabled = false;
}
if (pds.currentpageindex == pds.pagecount-1)
...{
this.lbtnnextpage.enabled = false;
this.lbtndownpage.enabled = false;
}
repeater1.databind();
}
protected void lbtnpritpage_click(object sender, eventargs e)
...{
this.labpage.text = convert.tostring(convert.toint32(labpage.text) - 1);
this.contrlrepeater();
}
protected void lbtnfirstpage_click(object sender, eventargs e)
...{
this.labpage.text = "1";
this.contrlrepeater();
}
protected void lbtndownpage_click(object sender, eventargs e)
...{
this.labpage.text =this.labcountpage.text;
this.contrlrepeater();
}
protected void lbtnnextpage_click(object sender, eventargs e)
...{
this.labpage.text = convert.tostring(convert.toint32(labpage.text) + 1);
this.contrlrepeater();
}
}
--------------------------------------------------------
aspx文件:
<% @ control language = " c# " autoeventwireup = " true " codefile = " gonggao.ascx.cs " inherits = " gonggao " %>
< table >
< asp:repeater id = " repeater1 " runat = " server " >
< itemtemplate >
< tr >< td >< a href = "" ><% # cuts(databinder.eval(container.dataitem, " [标题] " ).tostring(), 8 ) %></ a ></ td >
< td >< a href = "" ><% # cuts(databinder.eval(container.dataitem, " [时间] " ).tostring(), 5 ) %></ a ></ td ></ tr >
</ itemtemplate >
</ asp:repeater >
</ table >
< table border = " 1 " >< tr >< td align = " center " >
< asp:linkbutton id = " lbtnfirstpage " runat = " server " onclick = " lbtnfirstpage_click " > 页首 </ asp:linkbutton >
< asp:linkbutton id = " lbtnpritpage " runat = " server " onclick = " lbtnpritpage_click " > 上一页 </ asp:linkbutton >
< asp:linkbutton id = " lbtnnextpage " runat = " server " onclick = " lbtnnextpage_click " > 下一页 </ asp:linkbutton >
< asp:linkbutton id = " lbtndownpage " runat = " server " onclick = " lbtndownpage_click " > 页尾 </ asp:linkbutton >< br />
第 < asp:label id = " labpage " runat = " server " text = " label " ></ asp:label > 页 / 共 < asp:label id = " labcountpage " runat = " server " text = " label " ></ asp:label > 页 跳至 < asp:dropdownlist
id = " dropdownlist1 " runat = " server " >
</ asp:dropdownlist >
</ td ></ tr >
</ table >
附:pb.returnds(根据sql语句返回dataset数据集.temptable表)
public dataset returnds( string sqlstr)
... {
dataset ds = new dataset();
try
...{
//conn.open();
sqlcommand comm = new sqlcommand(sqlstr, conn);
comm.commandtimeout = 20;
this.conn.open();
sqldataadapter sda = new sqldataadapter(comm);
sda.fill(ds, "temptable");
return ds;
conn.close(); conn.dispose(); comm.dispose();
}
catch (exception e)
...{
throw (e);
// ds = null;
return ds;
}
finally
...{
this.conn.close();
}
}