//第一次请求执行
if(!page.ispostback)
{
listbind();
currentpage = 0;
viewstate["pageindex"] = 0;
//计算总共有多少记录
recordcount = calculaterecord();
lblrecordcount.text = recordcount.tostring();
//计算总共有多少页
pagecount = recordcount/pagesize;
lblpagecount.text = pagecount.tostring();
viewstate["pagecount"] = pagecount;
}
}
//计算总共有多少条记录
public int calculaterecord()
{
int intcount;
string strcount = "select count(*) as co from table1";
sqlcommand mycomm = new sqlcommand(strcount,myconn);
sqldatareader dr = mycomm.executereader();
if(dr.read())
{
//intcount = int32.parse(dr["co"].tostring());
intcount=(int)dr["co"];
}
else
{
intcount = 0;
}
dr.close();
return intcount;
}
icollection createsource()
{
int startindex;
//设定导入的起终地址
startindex = currentpage*pagesize;
string strsel = "select * from table1";
dataset ds = new dataset();
sqldataadapter myadapter = new sqldataadapter(strsel,myconn);
myadapter.fill(ds,startindex,pagesize,"score");
return ds.tables["score"].defaultview;
}
public void listbind()
{
score.datasource = createsource();
score.databind();
lbnnextpage.enabled = true;
lbnprevpage.enabled = true;
if(currentpage==(pagecount-1)) lbnnextpage.enabled = false;
if(currentpage==0) lbnprevpage.enabled = false;
lblcurrentpage.text = (currentpage+1).tostring();
}
public void page_onclick(object sender,commandeventargs e)
{
currentpage = (int)viewstate["pageindex"];
pagecount = (int)viewstate["pagecount"];
string cmd = e.commandname;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(currentpage<(pagecount-1)) currentpage++;
break;
case "prev":
if(currentpage>0) currentpage--;
break;
}
viewstate["pageindex"] = currentpage;
listbind();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:label id="lblrecordcount" forecolor="red" runat="server" />条记录
当前为<asp:label id="lblcurrentpage" forecolor="red" runat="server" />/<asp:label id="lblpagecount" forecolor="red" runat="server" />页
<asp:datalist id="score" runat="server"
headerstyle-backcolor="#aaaadd"
alternatingitemstyle-backcolor="gainsboro"
edititemstyle-backcolor="yellow"
>
<itemtemplate>
姓名:<%# databinder.eval(container.dataitem,"title") %>
<asp:linkbutton id="btnselect" text="编辑" commandname="edit" runat="server" />
</itemtemplate>
</asp:datalist>
共<%=pagecount%>页
<asp:linkbutton id="lbnprevpage" text="上一页" commandname="prev" oncommand="page_onclick" runat="server" />
<asp:linkbutton id="lbnnextpage" text="下一页" commandname="next" oncommand="page_onclick" runat="server" />
</form>
</body>
</html>
新闻热点
疑难解答
图片精选