这里我主要是想分析下这里怎么实现多个数据在页面之间传递。这里要运用到是因为在会员确定要购买物品时候我们会生成一个清单供会员确定,而这里的信息是由上个页面传递过来的,如果用url传递恐怕不太合适。这里采用了一种新的的方法。还记得在电子商务之数据存储流程(五)我们有些页面是继承的BasePage类,而不是System.Web.UI.Page类。这里BasePage类是我们编写的,它也是继承了System.Web.UI.Page类,代码如下
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Shop.Common;
/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage:System.Web.UI.Page
{
internal const string KEY_CURRENTUSER = "Current Logged In User";
internal const string KEY_CURRENTORDER = "Current Order";
public BasePage()
{
}
public OrdersEntity CurrentOrder
{
get
{
try
{
return (OrdersEntity)(Session[KEY_CURRENTORDER]);
}
catch
{
return (null); // for design time
}
}
set
{
if (value == null)
{
Session.Remove(KEY_CURRENTORDER);
}
else
{
Session[KEY_CURRENTORDER] = value;
}
}
}
public EndUserEntity CurrentEndUser
{
get
{
try
{
return (EndUserEntity)(Session[KEY_CURRENTUSER]);
}
catch
{
return (null); // for design time
}
}
set
{
if (value == null)
{
Session.Remove(KEY_CURRENTUSER);
}
else
{
Session[KEY_CURRENTUSER] = value;
}
}
}
}
可以看出来实质是将数据存储在session中。例如我们在登录界面中可以将得到EndUser存储到base.CurrentEndUser
在从另外一个页面类读取base.CurrentEndUser即可。
另外说明下当游客结账变为会员的步骤,学习灵活的利用Cookies存储URL
当我们点击结账按钮会发生:
protected
void
commandCheckout_Click(
object
sender, EventArgs e)
{
Response.Cookies[
"
ReturnURL
"
].Value
=
"
CheckOut.aspx
"
;
Response.Redirect(
"
Login.aspx
"
);
}
当我们登录成功会发生:
Code
protected void commandLogin_Click( object sender , EventArgs e )
{
//所有控件都得到验证
if ( IsValid )
{
EndUserEntity enduser = new EndUserEntity();
ProcessEndUserLogin processlogin = new ProcessEndUserLogin();
enduser.UserContactInformation.Email = textUsername.Text;
enduser.Password = textPassword.Text;
processlogin.EndUser = enduser;
try
{
processlogin.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}
//验证成功是会员
if ( processlogin.IsAuthenticated )
{
Response.Cookies["Authenticated"].Value = "True";
//存入到session中去
base.CurrentEndUser = processlogin.EndUser;
//如果用户是强制转过来登录此刻就让他返回原来的页面
if ( Request.Cookies["ReturnURL"] != null )
{
Response.Redirect( Request.Cookies["ReturnURL"].Value );
}
//就返回到默认的页面,也就是用户订单界面
else
{
Response.Redirect("Account/CustomerOrders.aspx");
}
}
else
{
labelMessage.Text = "Invalid login!";
}
}
这里还分析一下它使用GridView的方法,不像我们平时那样,在每一行添加一个选择,删除,更新命名字段
代码
<
asp:GridView
ID
="gvwBigKind"
runat
="server"
AutoGenerateColumns
="False"
OnRowEditing
="gvwBigKind_RowEditing"
OnRowUpdating
="gvwBigKind_RowUpdating"
OnRowCancelingEdit
="gvwBigKind_RowCancelingEdit"
OnRowDeleting
="gvwBigKind_RowDeleting"
OnSelectedIndexChanging
="gvwBigKind_SelectedIndexChanging"
>
<
Columns
>
<
asp:CommandField
HeaderText
="编辑"
ShowEditButton
="True"
ShowHeader
="True"
/>
<
asp:CommandField
HeaderText
="选择"
ShowSelectButton
="True"
/>
<
asp:CommandField
HeaderText
="删除"
ShowDeleteButton
="True"
DeleteText
="<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div> "
/>
<
asp:BoundField
DataField
="总类型编号"
HeaderText
="总类型编号"
ReadOnly
="true"
/>
<
asp:BoundField
DataField
="总类型名称"
HeaderText
="总类型名称"
/>
</
Columns
>
</
asp:GridView
>
这里没有在每一行开头给出编辑,删除按钮,却能完成上面的功能
代码
<
asp:GridView
ID
="gridviewShoppingCart"
runat
="server"
AutoGenerateColumns
="false"
DataKeyNames
="Quantity,ShoppingCartID"
OnRowDataBound
="gridviewShoppingCart_RowDataBound"
Width
="100%"
BorderWidth
="0px"
CellPadding
="2"
ShowHeader
="false"
>
<
Columns
>
<
asp:TemplateField
ItemStyle-Width
="16%"
ItemStyle-HorizontalAlign
="center"
>
<
ItemTemplate
>
<
asp:CheckBox
ID
="checkboxDelete"
runat
="server"
/>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
ItemStyle-Width
="30%"
>
<
ItemTemplate
>
<%
#
Eval
(
"
ProductName
"
)
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
ItemStyle-Width
="17%"
ItemStyle-HorizontalAlign
="center"
>
<
ItemTemplate
>
<
asp:TextBox
id
="textQuantity"
runat
="server"
Columns
="4"
MaxLength
="3"
Text
='<%#
Eval("Quantity") %
>
' width="30px" CssClass="textfield" />
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
ItemStyle-Width
="18%"
ItemStyle-HorizontalAlign
="center"
>
<
ItemTemplate
>
<%
#
Eval
(
"
UnitPrice
"
,
"
{0:c}
"
)
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
ItemStyle-Width
="19%"
ItemStyle-HorizontalAlign
="center"
>
<
ItemTemplate
>
<%
#
Eval
(
"
TotalPrice
"
,
"
{0:c}
"
)
%>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
还加上一个更新按钮,这里在GridView中checkbox打上勾的不会立即删除,而是点击更新按钮之后会更新。如果要改变商品数量直接重新输入在点击更新即可
代码
///
<summary>
///
通过循环来更新购物车
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
commandUpdate_Click(
object
sender, EventArgs e)
{
foreach
(GridViewRow row
in
gridviewShoppingCart.Rows)
{
if
(row.RowType
==
DataControlRowType.DataRow)
{
DataKey data
=
gridviewShoppingCart.DataKeys[row.DataItemIndex];
CheckBox check
=
(CheckBox)row.FindControl(
"
checkboxDelete
"
);
if
(check.Checked)
{
Delete(
int
.Parse(data.Values[
"
ShoppingCartID
"
].ToString()));
}
TextBox textNewQuantity
=
(TextBox)row.FindControl(
"
textQuantity
"
);
int
integerNewQuantity
=
int
.Parse(textNewQuantity.Text);
//
这里可以获得原来
int
integerOrigQuantity
=
int
.Parse(gridviewShoppingCart.DataKeys[row.DataItemIndex].Value.ToString());
//
数量不相等证明购物车中商品数量改变了,就要实行刷新
if
(integerNewQuantity
!=
integerOrigQuantity)
{
Update(
int
.Parse(data.Values[
"
ShoppingCartID
"
].ToString()), integerNewQuantity);
}
}
}
//
重新绑定数据
LoadShoppingCart();
}
private
void
Update(
int
id,
int
newqty)
{
ProcessUpdateShoppingCart processupdate
=
new
ProcessUpdateShoppingCart();
ShoppingCartEntity shoppingcart
=
new
ShoppingCartEntity();
shoppingcart.Quantity
=
newqty;
shoppingcart.ShoppingCartID
=
id;
processupdate.ShoppingCart
=
shoppingcart;
try
{
processupdate.Invoke();
}
catch
{
Response.Redirect(
"
ErrorPage.aspx
"
);
}
}
private
void
Delete(
int
id)
{
ProcessDeleteShoppingCart processdelete
=
new
ProcessDeleteShoppingCart();
ShoppingCartEntity shoppingcart
=
new
ShoppingCartEntity();
shoppingcart.ShoppingCartID
=
id;
processdelete.ShoppingCart
=
shoppingcart;
try
{
processdelete.Invoke();
}
catch
{
Response.Redirect(
"
ErrorPage.aspx
"
);
}
}
这个电子商务系统就分析到这里吧。如果需要原书中代码可以点击这里下载
另外需要注明自己在随笔中修改书中多代码,并删掉了支付这一段。