APSNET沈士根实验报告DOCWord格式文档下载.docx
- 文档编号:21522634
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:17
- 大小:147.71KB
APSNET沈士根实验报告DOCWord格式文档下载.docx
《APSNET沈士根实验报告DOCWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《APSNET沈士根实验报告DOCWord格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。
MyPetShopDataContextdb=newMyPetShopDataContext();
//Page_Load事件,将Category表中的CategoryId和Name字段值填充到ddlCategory下拉列表框,执行方法代码如下。
protectedvoidPage_Load(objectsender,EventArgse)
if(!
IsPostBack)
varcategories=fromcindb.Category
selectnew
c.CategoryId,
c.Name
};
foreach(varcategoryincategories)
ddlCategory.Items.Add(newListItem(category.Name.ToString(),category.CategoryId.ToString()));
}
Bind();
//编写自定义方法Bind(),该方法根据选择的CategoryId显示分类中包含的商品。
privatevoidBind()
intcategoryId=int.Parse(ddlCategory.SelectedValue);
varproducts=frompindb.Productwherep.CategoryId==categoryIdselectp;
gvProduct.DataSource=products;
gvProduct.DataBind();
//当改变ddlCategory中的分类名后,触发SelectedIndexChanged事件,此时,需要重新在gvProduct中显示该分类名包含的商品
protectedvoidddlCategory_SelectedIndexChanged(objectsender,EventArgse)
//当改变gvProduct的当前页后,触发PageIndexChanging事件,此时,需要重新设置新的页面索引值。
protectedvoidgvProduct_PageIndexChanging(objectsender,GridViewPageEventArgse){
gvProduct.PageIndex=e.NewPageIndex;
6.设计Profile
操作时,打开MyPetShop网站根文件夹下的Web.config,在<
system.web>
元素中,编写配置代码如下:
<
anonymousIdentificationenabled="
true"
/>
profile>
properties>
groupname="
Cart"
>
addname="
ProId"
type="
System.Collections.ArrayList"
allowAnonymous="
/>
ProName"
allowAnonymous="
addname="
Qty"
ListPrice"
TotalPrice"
/group>
/properties>
/profile>
7.设计ShopCart.aspx如图:
其中的Columms属性设置如图:
详细设置见书本。
8.编写ShopCart.aspx.cs中的方法代码
Web窗体载入时,触发Page.load事件,将判断从ProShow.aspx传递过来的ProductId是否为空值,若非空,则获取ProductId值,再将ProductId值对应的商品信息添加到购物车。
然后,显示购物车中包含的商品数据。
代码如下:
if(Request.QueryString["
ProductId"
]!
=null)
//自定义方法AddProduct(intid),将指定商品编号的商品添加到购物车:
protectedvoidAddProduct(intid)
intisExit=0;
for(intj=0;
j<
Profile.Cart.ProName.Count;
j++)
if(id==(int)Profile.Cart.ProId[j])
ints=(int)Profile.Cart.Qty[j];
s++;
Profile.Cart.Qty[j]=s;
Profile.Save();
isExit=1;
varproduct=(frompindb.Productwherep.ProductId==idselectp).First();
Profile.Cart.ListPrice.Add(product.ListPrice);
Profile.Cart.Qty.Add
(1);
Profile.Cart.ProId.Add(product.ProductId);
Profile.Cart.ProName.Add(product.Name);
Profile.Save();
//编写自定义方法Bind(),该方法将Profile.Cart中的所有购物记录存放到一个数据表dt中,再将dt作为数据源,绑定到gvCart。
protectedvoidBind()
Profile.Cart.TotalPrice=TotalPrice().ToString();
lblTotalPrice.Text=Profile.Cart.TotalPrice;
DataTabledt=newDataTable();
dt.Columns.Add("
ProId"
);
ProName"
);
ListPrice"
Qty"
for(inti=0;
i<
i++)
DataRowrow=dt.NewRow();
row[0]=Profile.Cart.ProId[i];
row[1]=Profile.Cart.ProName[i];
row[2]=Profile.Cart.ListPrice[i];
row[3]=Profile.Cart.Qty[i];
dt.Rows.Add(row);
gvCart.DataSource=dt;
gvCart.DataBind();
//编写自定义方法TotalPrice(),该方法用于计算机购物车中的购物总金额
protecteddecimalTotalPrice()
decimalsum=0;
for(intj=0;
j<
j++){
intqty=(int)Profile.Cart.Qty[j];
decimallistPrice=(decimal)Profile.Cart.ListPrice[j];
sum+=qty*listPrice;
事件
protectedvoidbtnDelete_Click(objectsender,EventArgse)
intproductId=0;
gvCart.Rows.Count;
i++){
CheckBoxchkProduct=newCheckBox();
chkProduct=(
CheckBox)gvCart.Rows[i].FindControl("
chkProduct"
if(chkProduct!
if(chkProduct.Checked)
int.Parse(gvCart.Rows[i].Cells[1].Text);
productId=
DeleteProduct(productId);
//自定义方法DeleteProduct(intid),用于在购物车中删除指定购物记录
protectedvoidDeleteProduct(intid)
intj=0;
for(inti=0;
if(id==(int)Profile.Cart.ProId[i])
Profile.Cart.ListPrice.RemoveAt(j);
Profile.Cart.ProId.RemoveAt(j);
Profile.Cart.ProName.RemoveAt(j);
Profile.Cart.Qty.RemoveAt(j);
//按钮btnClear的Click事件protectedvoidbtnClear_Click(objectsender,EventArgse)
Profile.Cart.Qty.Clear();
Profile.Cart.ProName.Clear();
Profile.Cart.ProId.Clear();
Profile.Cart.ListPrice.Clear();
Response.Redirect("
ProShow.aspx"
//按钮btnComputeAgain的Click事件protectedvoidbtnComputeAgain_Click(objectsender,EventArgse)
lblError.Text="
"
;
TextBoxtxtQty=newTextBox();
txtQty=(TextBox)gvCart.Rows[i].FindControl("
txtQty"
if(txtQty!
varproduct=(frompindb.Productwherep.ProductId==
int.Parse(gvCart.Rows[i].Cells[1].Text)selectp).First();
if(int.Parse(txtQty.Text)>
product.Qty)
{lblError.Text+="
Error:
库存不足,商品名为"
+
gvCart.Rows[i].Cells[2].Text+"
的库存量为"
+product.Qty.ToString()+"
br/>
}else
{ChangeQty(int.Parse(gvCart.Rows[i].Cells[1].Text),int.Parse(txtQty.Text));
//自定义方法ChangeQty,根据指定商品编号修改对应的购买数量protectedvoidChangeQty(intid,intqty)
if(id==(int)Profile.Cart.ProId[i])
Profile.Cart.Qty[i]=qty;
//按钮btnSettle的Click事件protectedvoidbtnSettle_Click(objectsender,EventArgse){
SubmitCart.aspx"
//按钮btnContinue的Click事件
protectedvoidbtnContinue_Click(objectsender,EventArgse){
9.设计SumbitCart.aspx。
10.编写SumbitCart.aspx.cs中的方法代码:
//按钮btnContinue
的Click事件
protectedvoidbtnContinueClick1(
objectsender,EventArgse)
Response.Redirect(
//按钮btnSubmit的Click事件,首先在Order表中添加一天订单,其次在OrderItem表中添加该订单的详细信息,然后修改Product表的商品库存,最后清空Profile.Cart中的各数组列表对象
protectedvoidbtnSubmit_Click1(objectsender,EventArgse)
Orderorder=newOrder();
order.UserName="
张三"
;
order.OrderDate=DateTime.Now;
order.Addr1=txtZip.Text.Trim();
order.Zip=txtZip.Text.Trim();
order.Phone=txtPhone.Text.Trim();
order.Status="
未审核"
db.Order.InsertOnSubmit(order);
db.SubmitChanges();
intid=order.OrderId;
OrderItemorderItem=newOrderItem();
orderItem.OrderId=id;
orderItem.ProName=(
string)Profile.Cart.ProName[i];
orderItem.ListPrice=(
decimal)Profile.Cart.ListPrice[i];
orderItem.Qty=(
int)Profile.Cart.Qty[i];
orderItem.TotalPrice=(
int)Profile.Cart.Qty[i]*
(int)Profile.Cart.ListPrice[i];
db.OrderItem.InsertOnSubmit(orderItem);
db.SubmitChanges();
varproduct=(fromcindb.Productwherec.ProductId==(int)Profile.Cart.ProId[i]selectc).First();
product.Qty-=orderItem.Qty;
Profile.Cart.ProName.Clear();
Profile.Cart.ListPrice.Clear();
Profile.Cart.TotalPrice="
lblMsg.Text="
已经成功结算,谢谢光临!
"
11.从浏览ProShow.aspx开始对MyPetShop进行测试。
2.设利用DetailsView
控件实现数据插入、编辑、删除等操作
(1)设计Web窗体:
(2)①添加一个DetailsView控件和三个LinqDataSource控件,分别设其ID的属性值为:
dvProduct、ldsProduct、ldsCategory和ldsSupplier
2:
如图配置ldsProduct的数据源为Product表。
3配置Category表如同步骤②
4配置Supplier表如同步骤②
5单击dvProduct的智能标记,选择数据源为ldsProduct启用分页插入、编辑、删除,单击“编辑字段”将CategoryId和SuppId字段转化为TemplateField字段
⑥配置CategoryId和SuppId字段如下图:
⑥浏览效果如下:
实验八拓展
(1)修改ProShow.aspx页面,要求增加“商品编号”和“商品分类号”的显示。
此步骤以在前面的实验中实现,在ItemTemple中插入两列如图:
然后再将Lable
之后再编辑中输入
(三)实验心得
1、遇到的问题和解决方法:
①实验中经常遇到SQL数据库版本过低不能加载的问题,也不知道为什么。
重启之后解决了。
2、收获:
回顾本次的实验目的,首先是要我们掌握ListControl类控件与数据源的绑定方法,其实在前面的有一章已经使用过该控件,而经过这次制作学会了如何在ListControl类控件中显示数据库数据。
接着是要我们熟练掌握GridView控件的应用,实验中让我体会最深的就是这个控件用来显示二维表格式的数据,它能很方便的实现数据绑定、分页、排序、行选择、更新、删除等等功能。
最后是DetailsView控件的应用,在制作前我浏览的书本,书本上的介绍是此控件以表格型式显示和处理来自数据库的单条记录,特点就是表格只包含两个数据列。
一个逐行显示各字段名,一个显示对应字段名的数据值。
与GridView相比较就是多了
《Web编程基础》实验报告一个数据插入的功能。
在做完之后我发现了其实结合GridView和DetailsView管理数据是非常方便的一个用于显示一个用于修改。
本次实验的编码的数据量比前面几个实验都要大,但用心做其实也不是非常难。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- APSNET 沈士根 实验 报告 DOC