数据库连接代码:
using system;
using system.data;
using system.configuration;
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;
using system.data.sql;
public partial class _default : system.web.ui.page
{
private void con()
{
sqlconnection con = new sqlconnection("server='.';database=test;uid=sa;pwd=123456;");
sqlconnection conn = new sqlconnection();
dataset ds = new dataset();
sqldataadapter sda = new sqldataadapter("select * from news", con);
sda.fill(ds, "newstitle");
//sqldataadapter sda2 = new sqldataadapter("select * from prospectivebuyer", con);
// sda2.fill(ds, "title");
pageddatasource pds = new pageddatasource();
pds.datasource = ds.tables["name"].defaultview;
//pageddatasource aa = new pageddatasource();
pds.allowpaging = true;//允许分页
pds.pagesize = 8;//单页显示项数
int curpage;
if (request.querystring["page"] != null)
curpage = convert.toint32(request.querystring["page"]);
else
curpage = 1;
pds.currentpageindex = curpage - 1;
int count = pds.pagecount;
lblcurrentpage.text = "当前页:" + curpage.tostring();
labpage.text = count.tostring();
if (!pds.isfirstpage)
{
this.first.navigateurl = request.currentexecutionfilepath + "?page=1";
this.last.navigateurl = request.currentexecutionfilepath + "?page=" + convert.tostring(count - 1); ;
up.navigateurl = request.currentexecutionfilepath + "?page=" + convert.tostring(curpage - 1);
}
this.first.visible = false ;
this.last.visible = false ;
if (!pds.islastpage)
next.navigateurl = request.currentexecutionfilepath + "?page=" + convert.tostring(curpage + 1);
this.first.visible = false;
this.last.visible = false;
repeater1.datasource = pds ;
repeater1.databind();
}
protected void page_load(object sender, eventargs e)
if (!ispostback)
con();
this.first.visible = true;
this.last.visible = true;
//this.repeater1.datasource = pds();
//this.repeater1.databind();
}
aspx文件代码:
<table>
<tr ><td class="style1" align ="left" >hehe</td></tr>
<tr ><td class="style1">
<asp:repeater id="repeater1" runat="server" >
<headertemplate ><table><tr><td>头模板</td></tr></headertemplate>
<itemtemplate ><tr><td ><font color="red" > <%#("timekey")%></font></td></tr></itemtemplate>
<alternatingitemtemplate ><tr><td > <a href ='default.aspx?id=<%#"databaselogid" %>'><%#("salesamountquota")%></a></td></tr></alternatingitemtemplate>
<footertemplate ><tr><td>尾模板</td></tr></table></footertemplate>
</asp:repeater>
</td> </tr>
<tr> <td class="style1">
<asp:hyperlink id="first" runat="server">首页</asp:hyperlink>
<asp:hyperlink id="next" runat="server">下一页</asp:hyperlink>
<asp:hyperlink id="up" runat="server">上一页</asp:hyperlink>
<asp:hyperlink id="last" runat="server">末页</asp:hyperlink>
</td></tr>
<tr> <td class="style1">当前页为:<asp:label id="lblcurrentpage" runat="server"
text="label"></asp:label>&nbsp;
共<asp:label id="labpage" runat="server" text="label"></asp:label>
页</td><td class="style1" style="height: 21px">
<asp:hyperlink id="next" runat="server">下一页</asp:hyperlink>
</table>
第二种方式:
using system.collections;
using system.linq;
using system.xml.linq;
public partial class databind : system.web.ui.page
num.text = "1";
repdatabind();
public void repdatabind()
sqlconnection con = new sqlconnection("server='.';database=test;uid=sa;pwd=123456;");
sqldataadapter sda = new sqldataadapter("select * from dimproduct", con);
sda.fill(ds, "name");
int curpage = convert.toint32(num.text);
this.btndown.enabled = true;
this.btnup.enabled = true;
if (curpage == 1)
this.btnup.enabled = false;
if (curpage == pds.pagecount)
this.btndown.enabled = false;
this.repeater1.datasource = pds;
this.repeater1.databind();
protected void btnup_click(object sender, eventargs e)
this.num.text =convert.tostring ( convert.toint32(num.text)- 1) ;
repdatabind();
protected void btndown_click(object sender, eventargs e)
this.num.text = convert.tostring(convert.toint32(num.text)+ 1) ;
aspx代码:
<%@ page language="c#" codefile="databind.aspx.cs" inherits="databind" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:panel id="panel1" runat="server" height="173px">
<asp:repeater id="repeater1" runat="server"><headertemplate ><table border onmousedown="1" ><tr><td >头模板</td></tr></headertemplate><itemtemplate ><tr><td>序号:<%# ("productkey") %></td></tr><tr><td>编码:<%# ("productalternatekey") %></td></tr></itemtemplate><footertemplate ><tr><td>脚模板</td></tr></table></footertemplate>
</asp:repeater>
当前页:<asp:label id="num" runat="server"></asp:label>
<br />
<asp:button id="btnup" runat="server" onclick="btnup_click" text="上一页" />
<asp:button id="btndown" runat="server" onclick="btndown_click" text="下一页" />
</asp:panel>
<br />
</div>
</form>
</body>
</html>