不多说 直接贴代码
第一种方式:
数据库连接代码:
01 | usingSystem; |
02 | usingSystem.Data; |
03 | usingSystem.Configuration; |
04 | usingSystem.Web; |
05 | usingSystem.Web.Security; |
06 | usingSystem.Web.UI; |
07 | usingSystem.Web.UI.WebControls; |
08 | usingSystem.Web.UI.WebControls.WebParts; |
09 | usingSystem.Web.UI.HtmlControls; |
10 | usingSystem.Data.SqlClient; |
11 | usingSystem.Data.Sql; |
12 |
13 |
14 |
15 | publicpartialclass_Default : System.Web.UI.Page |
16 | { |
17 | privatevoid con() |
18 | { |
19 | stringconnstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString; |
20 | SqlConnection con = newSqlConnection(connstring); |
21 | SqlConnection conn = newSqlConnection(); |
22 | DataSet ds = newDataSet(); |
23 | SqlDataAdapter sda = newSqlDataAdapter("select * from FactSalesQuota", con); |
24 | sda.Fill(ds, "name"); |
25 | SqlDataAdapter sda2 = newSqlDataAdapter("select * from ProspectiveBuyer", con); |
26 | sda2.Fill(ds, "title"); |
27 | PagedDataSource pds = newPagedDataSource(); |
28 | pds.DataSource = ds.Tables["name"].DefaultView; |
29 | //PagedDataSource aa = new PagedDataSource(); |
30 | pds.AllowPaging = true;//允许分页 |
31 | pds.PageSize = 8;//单页显示项数 |
32 | intCurPage; |
33 | if(Request.QueryString["Page"] != null) |
34 | CurPage = Convert.ToInt32(Request.QueryString["Page"]); |
35 | else |
36 | CurPage = 1; |
37 | pds.CurrentPageIndex = CurPage - 1; |
38 | intCount = 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 | protectedvoidPage_Load(objectsender, 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> |