温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:我的小书坊源码(三层实现)
当前文件:
MyBookShop/BookAdd.aspx.cs,打开代码结构图
MyBookShop/BookAdd.aspx.cs,打开代码结构图1using System; 2
using System.Collections; 3
using System.ComponentModel; 4
using System.Data; 5
using System.Drawing; 6
using System.Web; 7
using System.Web.SessionState; 8
using System.Web.UI; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.HtmlControls; 11
12
using MyBookShop.BusinessLogicLayer; 13
using MyBookShop.BusinessLogicHelper; 14
15
namespace MyBookShop.Web 16
{ 17
/// <summary> 18
/// BookAdd 的摘要说明。 19
/// </summary> 20
public partial class BookAdd : System.Web.UI.Page 21
{ 22
23
protected void Page_Load(object sender, System.EventArgs e) 24
{ 25
if(!IsPostBack) 26
{ 27
InitData(); 28
//InputPictureFile.Accept="image/*"; 29
} 30
} 31
32
private void InitData() 33
{ 34
//初始化:类别下拉框中的数据,用Category表中的数据进行绑定 35
DataTable dt= Category.Query(new Hashtable()); 36
foreach(DataRow dr in dt.Rows) 37
{ 38
DropDownListCategory.Items.Add(new ListItem(dr["CategoryName"].ToString(),dr["CategoryId"].ToString())); 39
} 40
} 41
42
/// <summary> 43
/// 提交按钮单击事件 44
/// </summary> 45
/// <param name="sender"></param> 46
/// <param name="e"></param> 47
protected void ButtonOK_Click(object sender, System.EventArgs e) 48
{ 49
//构造book信息哈希表 50
Hashtable ht=new Hashtable(); 51
ht.Add("BookName",TextBoxBookName.Text.Trim()); 52
ht.Add("CategoryId",DropDownListCategory.SelectedValue); 53
ht.Add("Price",TextBoxPrice.Text.Trim()); 54
ht.Add("Publisher",TextBoxPublisher.Text.Trim()); 55
ht.Add("PublishDate",TextBoxPublishDate.Text.Trim()); 56
ht.Add("Author",TextBoxAuthor.Text.Trim()); 57
ht.Add("PageNum",TextBoxPageNum.Text.Trim()); 58
ht.Add("Description",TextBoxDescription.Text.Trim()); 59
60
//图片名,以当前时间为文件名,确保文件名没有重复 61
string uploadName=InputPictureFile.Value.Trim(); 62
string pictureName=""; 63
if(uploadName!="") 64
{ 65
int idx=uploadName.LastIndexOf("."); 66
string suffix=uploadName.Substring(idx);//文件后缀 67
//Ticks属性的值为自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。 68
pictureName=System.DateTime.Now.Ticks.ToString()+suffix; 69
ht.Add("PictureUrl",pictureName); 70
} 71
72
ht.Add("SaleCount",1); 73
74
//添加图书,如果数据类型不正确,给出提示. 75
ArrayList WarningMessageList=new ArrayList(); 76
LabelWarningMessage.Text=""; 77
if(BookHelper.Add(ht,ref WarningMessageList)==false) 78
{ 79
LabelWarningMessage.Text="<font color=red>"; 80
foreach(string item in WarningMessageList) 81
{ 82
LabelWarningMessage.Text+=item+"<br>"; 83
} 84
LabelWarningMessage.Text+="</font>"; 85
} 86
87
//上传图片到目录"\BookPics\"中 88
else 89
{ 90
if(uploadName!="") 91
{ 92
string path=Server.MapPath(".\\BookPics\\"); 93
InputPictureFile.PostedFile.SaveAs(path+pictureName); 94
} 95
Response.Redirect("BookList.aspx"); 96
} 97
98
} 99
} 100
} 101





}