第一种方式:

数据库连接代码:

01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Web;
05 using System.Web.Security;
06 using System.Web.UI;
07 using System.Web.UI.WebControls;
08 using System.Web.UI.WebControls.WebParts;
09 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Data.Sql;
12  
13  
14  
15 public partial class _Default : System.Web.UI.Page
16 {
17 private void con()
18 {
19 string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
20 SqlConnection con = new SqlConnection(connstring);
21 SqlConnection conn = new SqlConnection();
22 DataSet ds = new DataSet();
23 SqlDataAdapter sda = new SqlDataAdapter("select * from FactSalesQuota", con);
24 sda.Fill(ds, "name");
25 SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
26 sda2.Fill(ds, "title");
27 PagedDataSource pds = new PagedDataSource();
28 pds.DataSource = ds.Tables["name"].DefaultView;
29 //PagedDataSource aa = new PagedDataSource();
30 pds.AllowPaging = true;//允许分页
31 pds.PageSize = 8;//单页显示项数
32 int CurPage;
33 if (Request.QueryString["Page"] != null)
34 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
35 else
36 CurPage = 1;
37 pds.CurrentPageIndex = CurPage - 1;
38 int Count = pds.PageCount;
39  
40 lblCurrentPage.Text = "当前页:" + CurPage.ToString();
41 labPage.Text = Count.ToString();
42  
43 if (!pds.IsFirstPage)
44 {
45 this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
46 this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
47 up.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
48 }
49 else
50 {
51 this.first.Visible = false ;
52 this.last.Visible = false ;
53  
54 }
55  
56 if (!pds.IsLastPage)
57 {
58  
59  
60 next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
61 }
62 else
63 {
64 this.first.Visible = false;
65 this.last.Visible = false;
66  
67 }
68  
69 Repeater1.DataSource = pds ;
70 Repeater1.DataBind();
71  
72 }
73  
74  
75  
76 protected void Page_Load(object sender, EventArgs e)
77 {
78 if (!IsPostBack)
79 {
80 con();
81 this.first.Visible = true;
82 this.last.Visible = true;
83 //this.Repeater1.DataSource = pds();
84 //this.Repeater1.DataBind();
85  
86 }
87  
88 }
89 }

 

aspx文件代码:

01 <table>
02 <tr ><td class="style1" align ="left" >hehe</td></tr>
03  
04 <tr ><td class="style1">
05 <asp:Repeater ID="Repeater1" runat="server" >
06  
07 <HeaderTemplate ><table><tr><td>头模板</td></tr></HeaderTemplate>
08 <ItemTemplate ><tr><td ><font color="red" > <%#Eval("timekey")%></font></td></tr></ItemTemplate>
09 <AlternatingItemTemplate ><tr><td > <a href ='Default.aspx?id=<%#"databaselogid" %>'><%#Eval("SalesAmountQuota")%></a></td></tr></AlternatingItemTemplate>
10 <FooterTemplate ><tr><td>尾模板</td></tr></table></FooterTemplate>
11 </asp:Repeater>
12 </td> </tr>
13  
14  
15 <tr> <td class="style1">
16 <asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
17 <asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
18 <asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
19 <asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
20 </td></tr>
21  
22 <tr><td class="style1">当前页为:<asp:Label ID="lblCurrentPage" runat="server"
23 Text="Label"></asp:Label>
24 <br />
25 共<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>
26 页</td></tr>
27  
28 </table>

第二种方式:

01 using System;
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Data.SqlClient;
14  
15 public partial class databind : System.Web.UI.Page
16 {
17  
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 num.Text = "1";
23 repdatabind();
24 }
25  
26  
27 }
28 public void repdatabind()
29 {
30 string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
31 SqlConnection con = new SqlConnection(connstring);
32 SqlConnection conn = new SqlConnection();
33 DataSet ds = new DataSet();
34 SqlDataAdapter sda = new SqlDataAdapter("select * from DimProduct", con);
35 sda.Fill(ds, "name");
36 PagedDataSource pds = new PagedDataSource();
37 pds.DataSource = ds.Tables["name"].DefaultView;
38 pds.AllowPaging = true;//允许分页
39 pds.PageSize = 8;//单页显示项数
40  
41 int curpage = Convert.ToInt32(num.Text);
42 this.BtnDown.Enabled = true;
43 this.BtnUp.Enabled = true;
44 pds.CurrentPageIndex = curpage - 1;
45 if (curpage == 1)
46 {
47 this.BtnUp.Enabled = false;
48 }
49 if (curpage == pds.PageCount)
50 {
51 this.BtnDown.Enabled = false;
52 }
53 this.Repeater1.DataSource = pds;
54 this.Repeater1.DataBind();
55 }
56  
57 protected void BtnUp_Click(object sender, EventArgs e)
58 {
59 this.num.Text =Convert.ToString ( Convert.ToInt32(num.Text)- 1) ;
60 repdatabind();
61 }
62 protected void BtnDown_Click(object sender, EventArgs e)
63 {
64 this.num.Text = Convert.ToString(Convert.ToInt32(num.Text)+ 1) ;
65 repdatabind();
66 }
67 }

aspx代码:

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="databind.aspx.cs" Inherits="databind" %>
02  
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04  
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07 <title>无标题页</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <div>
12  
13 <asp:Panel ID="Panel1" runat="server" Height="173px">
14   <asp:Repeater ID="Repeater1" runat="server"><HeaderTemplate ><table border onmousedown="1" ><tr><td >头模板</td></tr></HeaderTemplate><ItemTemplate ><tr><td>序号:<%# Eval("ProductKey") %></td></tr><tr><td>编码:<%# Eval("ProductAlternateKey") %></td></tr></ItemTemplate><FooterTemplate ><tr><td>脚模板</td></tr></table>< /FooterTemplate>
15 </asp:Repeater>
16 当前页:<asp:Label ID="num" runat="server"></asp:Label>
17 <br />
18 <asp:Button ID="BtnUp" runat="server" onclick="BtnUp_Click" Text="上一页" />
19 <asp:Button ID="BtnDown" runat="server" onclick="BtnDown_Click" Text="下一页" />
20 </asp:Panel>
21 <br />
22 <br />
23  
24 </div>
25 </form>
26 </body>
27 </html>

posted on 2013-04-29 00:46  零点HY  阅读(147)  评论(1编辑  收藏  举报