|
Posted on
2008-08-29 20:02
管志鹏
阅读( 196)
评论()
编辑
收藏
举报
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
1 Books实体
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3 using System;
4 using System.Data;
5 using System.Configuration;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//// <summary>
14 /// Books 的摘要说明
15 /// </summary>
16 public class Books
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
18 public Books()
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 private int id;
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26 public int Id
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return id; }
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { id = value; }
30 }
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32 private string title;
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34 public string Title
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return title; }
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { title = value; }
38 }
39 private string author;
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41 public string Author
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return author; }
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { author = value; }
45 }
46 private string contentDescription;
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48 public string ContentDescription
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return contentDescription; }
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { contentDescription = value; }
52 }
53 private string pic;
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55 public string Pic
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return pic; }
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { pic = value; }
59 }
60 private double unitPrice;
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62 public double UnitPrice
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return unitPrice; }
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { unitPrice = value; }
66 }
67 }
68![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69 BookOperator类
70![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71 using System;
72 using System.Data;
73 using System.Configuration;
74 using System.Web;
75 using System.Web.Security;
76 using System.Web.UI;
77 using System.Web.UI.WebControls;
78 using System.Web.UI.WebControls.WebParts;
79 using System.Web.UI.HtmlControls;
80![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81 using System.Data.SqlClient;
82 using System.Collections.Generic;
83![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//// <summary>
85 /// BooksOperator 的摘要说明
86 /// </summary>
87 public class BooksOperator
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
89 public BooksOperator()
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
91 //
92 // TODO: 在此处添加构造函数逻辑
93 //
94 }
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96 public static IList<Books> GetAllBooks()
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
98 IList<Books> bookList = new List<Books>();
99 Books book = null;
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101 string conStr = "server=.;database=bookshop;uid=sa;pwd=1234";
102 string sqlStr = "select id,title,author,contentDescription,pic,unitPrice from books ";
103 using (SqlConnection sqlConn = new SqlConnection(conStr))
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
105 sqlConn.Open();
106 SqlCommand sqlCmd = new SqlCommand(sqlStr,sqlConn);
107 SqlDataReader reader = sqlCmd.ExecuteReader();
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109 while (reader.Read())
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
111 book = new Books();
112 book.Id = reader.GetInt32(0);
113 book.Title = reader.GetString(1);
114 book.Author = reader.GetString(2);
115 book.ContentDescription = reader.GetString(3);
116 book.Pic = reader.GetString(4);
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118 book.UnitPrice =double.Parse(reader.GetDecimal(5).ToString());
119 bookList.Add(book);
120 }
121 reader.Close();
122 }
123 return bookList;
124 }
125 }
126 BookList.aspx
127![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
128 <%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
129![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
130 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
131![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
132 <html xmlns="http://www.w3.org/1999/xhtml" >
133 <head runat="server">
134 <title>无标题页</title>
135 </head>
136 <body>
137 <form id="form1" runat="server">
138 <div>
139 <asp:DataList ID="dlBookList" runat="server" Width="268px">
140
141 <ItemTemplate>
142 <table width="600">
143
144 <tr>
145 <td rowspan="4">
146 <asp:Image ID="Image1" ImageUrl='<%# "~/Images/"+Eval("Pic") %>' runat="server" />
147 </td>
148 <td><%# Eval("Title") %></td>
149 </tr>
150 <tr>
151
152 <td><%# Eval("Author") %></td>
153 </tr>
154 <tr>
155
156 <td><%# Eval("ContentDescription")%></td>
157 </tr>
158 <tr>
159
160 <td><%# Eval("UnitPrice")%></td>
161 </tr>
162
163 </table>
164
165 </ItemTemplate>
166 </asp:DataList>
167 <asp:Label ID="lblInfo" runat="server" Text=""></asp:Label>
168 <asp:Button ID="btnPre" runat="server" Text="前一页" OnClick="btnPre_Click" />
169 <asp:Button ID="btnNext" runat="server" Text="后一页" OnClick="btnNext_Click" />
170 </div>
171 </form>
172 </body>
173 </html>
174![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
175 页面后置类
176 BookList.aspx.cs
177![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
178
179![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
180 using System;
181 using System.Data;
182 using System.Configuration;
183 using System.Web;
184 using System.Web.Security;
185 using System.Web.UI;
186 using System.Web.UI.WebControls;
187 using System.Web.UI.WebControls.WebParts;
188 using System.Web.UI.HtmlControls;
189![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
190 using System.Data.SqlClient;
191 using System.Collections.Generic;
192 public partial class _Default : System.Web.UI.Page
193![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
194 //用来自定义分类的类
195 PagedDataSource pds;
196 protected void Page_Load(object sender, EventArgs e)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
198 if (!IsPostBack)
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
200 //用ViewState 保存当前页面的状态,这里保存的是当前页
201 ViewState["curPage"] = 0;
202
203 DataBindDBook();
204 }
205
206 }
207![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
208![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
209 /// 数据绑定的方法
210 /// </summary>
211 public void DataBindDBook()
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
213![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
214 pds = new PagedDataSource();
215
216 //允许分页
217 pds.AllowPaging = true;
218 //每页的大小
219 pds.PageSize = 2;
220 //当前页的索引
221 pds.CurrentPageIndex = CurPage;
222 //数据源
223 pds.DataSource = BooksOperator.GetAllBooks();
224 //设置 dlBookList 的数据源为 :PagedDataSource(pds)
225 this.dlBookList.DataSource = pds;
226 //调用DataBind()方法进行绑定
227 this.dlBookList.DataBind();
228![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
229 this.lblInfo.Text = string.Format("当前第{0}页,共{1}页",this.pds.CurrentPageIndex+1, this.pds.PageCount.ToString());
230 SetEnable();
231
232 }
233
234 //前一页按钮
235 protected void btnPre_Click(object sender, EventArgs e)
236![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
237 CurPage--;
238 //重新调用绑定的方法
239 DataBindDBook();
240
241 }
242
243 //下一页按钮
244 protected void btnNext_Click(object sender, EventArgs e)
245![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
246 CurPage++;
247 DataBindDBook();
248
249 }
250
251 //设置前一页,后一页的按钮是否有效
252 public void SetEnable()
253![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
254 this.btnPre.Enabled = true;
255 this.btnNext.Enabled = true;
256![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
257 if (this.pds.IsFirstPage)
258![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
259 this.btnPre.Enabled = false;
260 }
261 if (this.pds.IsLastPage)
262![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
263 this.btnNext.Enabled = false;
264 }
265 }
266![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
267 //属性: 当前页
268 public int CurPage
269![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
270 get
271![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
272 return (int)ViewState["curPage"];
273 }
274 set
275![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
276 ViewState["curPage"] = value;
277 }
278 }
279 }
280![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
281![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
282![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
283
284![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
|