1
Books实体2

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

13

/**//// <summary>14
/// Books 的摘要说明15
/// </summary>16
public class Books17


{18
public Books()19

{20
//21
// TODO: 在此处添加构造函数逻辑22
//23
}24
private int id;25

26
public int Id27

{28

get
{ return id; }29

set
{ id = value; }30
}31

32
private string title;33

34
public string Title35

{36

get
{ return title; }37

set
{ title = value; }38
}39
private string author;40

41
public string Author42

{43

get
{ return author; }44

set
{ author = value; }45
}46
private string contentDescription;47

48
public string ContentDescription49

{50

get
{ return contentDescription; }51

set
{ contentDescription = value; }52
}53
private string pic;54

55
public string Pic56

{57

get
{ return pic; }58

set
{ pic = value; }59
}60
private double unitPrice;61

62
public double UnitPrice63

{64

get
{ return unitPrice; }65

set
{ unitPrice = value; }66
}67
}68

69
BookOperator类70

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

81
using System.Data.SqlClient;82
using System.Collections.Generic;83

84

/**//// <summary>85
/// BooksOperator 的摘要说明86
/// </summary>87
public class BooksOperator88


{89
public BooksOperator()90

{91
//92
// TODO: 在此处添加构造函数逻辑93
//94
}95

96
public static IList<Books> GetAllBooks()97

{98
IList<Books> bookList = new List<Books>();99
Books book = null;100

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

{105
sqlConn.Open();106
SqlCommand sqlCmd = new SqlCommand(sqlStr,sqlConn);107
SqlDataReader reader = sqlCmd.ExecuteReader();108

109
while (reader.Read())110

{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

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.aspx127

128
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>129

130
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">131

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

175
页面后置类176
BookList.aspx.cs177

178
179

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

190
using System.Data.SqlClient;191
using System.Collections.Generic;192
public partial class _Default : System.Web.UI.Page 193


{194
//用来自定义分类的类195
PagedDataSource pds;196
protected void Page_Load(object sender, EventArgs e)197

{198
if (!IsPostBack)199

{200
//用ViewState 保存当前页面的状态,这里保存的是当前页201
ViewState["curPage"] = 0;202
203
DataBindDBook();204
}205
206
}207

208

/**//// <summary>209
/// 数据绑定的方法210
/// </summary>211
public void DataBindDBook()212

{213

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

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

{237
CurPage--;238
//重新调用绑定的方法239
DataBindDBook();240
241
}242
243
//下一页按钮244
protected void btnNext_Click(object sender, EventArgs e)245

{246
CurPage++;247
DataBindDBook();248
249
}250
251
//设置前一页,后一页的按钮是否有效252
public void SetEnable()253

{254
this.btnPre.Enabled = true;255
this.btnNext.Enabled = true;256

257
if (this.pds.IsFirstPage)258

{259
this.btnPre.Enabled = false;260
}261
if (this.pds.IsLastPage)262

{263
this.btnNext.Enabled = false;264
}265
}266

267
//属性: 当前页268
public int CurPage269

{270
get271

{272
return (int)ViewState["curPage"];273
}274
set275

{276
ViewState["curPage"] = value;277
}278
}279
}280

281

282

283
284

浙公网安备 33010602011771号