1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 using System.Data.SqlClient;
13
14
15
16
17 public partial class datalist3 : System.Web.UI.Page
18 {
19 int CurrentPage;//当前页数
20 int PageSize; //每页条数
21 int PageCount; //总页数
22 int RecordCount;//总条数
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 // 在此处放置用户代码以初始化页面
26
27
28 PageSize = 6;//每页10条记录
29
30
31 if (!Page.IsPostBack)
32 {
33 CurrentPage = 0;//当前页习惯设为0
34 ViewState["PageIndex"] = 0;//页索引也设为0
35
36
37 //计算总共有多少记录
38 RecordCount = CalculateRecord();
39
40
41 //计算总共有多少页
42 if (RecordCount % PageSize == 0)
43 {
44 PageCount = RecordCount / PageSize;
45 }
46 else
47 {
48 PageCount = RecordCount / PageSize + 1;
49 }
50
51 this.TotalLbl.Text = PageCount.ToString();//显示总页数
52 ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session
53
54 this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
55
56 }
57
58
59 }
60
61
62 //计算总共有多少条记录
63 private int CalculateRecord()
64 {
65 try
66 {
67 int recordCount;
68 SqlConnection con = new SqlConnection("server=192.168.10.100;database=Sura;user id=sa;password=13902195880;");//数据库使用Northwind;
69 con.Open();
70
71 //string sql = "select NewsId,NewsTitle,NewsContents,CreateDate from tblNews order by CreateDate DESC";
72 string sql = "select count(*) as count from tblNews";
73 SqlCommand cmd = new SqlCommand(sql, con);
74 SqlDataReader sdr = cmd.ExecuteReader();
75
76 if (sdr.Read())
77 {
78 recordCount = Int32.Parse(sdr["count"].ToString());
79 }
80
81
82 else
83 {
84 recordCount = 0;
85 }
86
87 sdr.Close();
88 con.Close();
89 return recordCount;
90 }
91
92
93 catch (Exception ex)
94 {
95 throw new Exception(ex.Message);
96 }
97 }
98
99
100 //将数据绑定到Datalist控件
101 public void DataListBind()
102 {
103 try
104 {
105 int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
106 string sql = "select * from tblNews";
107 DataSet ds = new DataSet();
108 SqlConnection con = new SqlConnection("server=192.168.10.100;database=Sura;user id=sa;password=13902195880;");
109 con.Open();
110
111 SqlDataAdapter sda = new SqlDataAdapter(sql, con);
112 sda.Fill(ds, StartIndex, PageSize, "NewsTitle");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
113 this.DataList1.DataSource = ds.Tables["NewsTitle"].DefaultView;
114 this.DataList1.DataBind();
115 this.PreviousLB.Enabled = true;
116 this.NextLB.Enabled = true;
117 if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
118 if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
119 this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
120
121 }
122
123
124 catch (Exception ex)
125 {
126 throw new Exception(ex.Message);
127 }
128 }
129
130
131
132
133 public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
134 {
135 CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
136 PageCount = (int)ViewState["PageCount"];//获得总页数
137
138
139 string cmd = e.CommandName;
140
141 //判断cmd,以判定翻页方向
142
143
144 switch (cmd)
145 {
146 case "prev"://上一页
147 if (CurrentPage > 0) CurrentPage--;
148 break;
149
150 case "next":
151 if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
152 break;
153
154 case "first"://第一页
155 CurrentPage = 0;
156 break;
157
158 case "end"://最后一页
159 CurrentPage = PageCount - 1;
160 break;
161
162 case "jump"://跳转到第几页
163 if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
164 {
165 return;
166 }
167 else
168 {
169 CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
170 break;
171 }
172 }
173 ViewState["PageIndex"] = CurrentPage;//获得当前页
174
175 this.DataListBind();//重新将DataList绑定到数据库
176
177
178 }
179
180
181 protected void JumpLB_Click(object sender, EventArgs e)
182 {
183
184 }
185 }