Order-Related Modules for an ASP.NET AJAX Server-Centric Based Online Shopping Website - Buying Goods
(Page 5 of 7 )
After clicking the "Add to shopping cart" button, the related item can be put into the shopping cart, all of which is finished within the ProductView_RowCommand click event handler. In conclusion, the function will accomplish the following tasks:
If there are no products inside the shopping cart, create an instance of the OrderInfo(i.e. order) class which is used to save the cart related info;
Obtain information about the product and create an instance of the OrderItemInfo(i.e. item) class to save this product-related info;
Add the item objectinto the OrderItemList listof the order object;
Update the info of the cart;
If there are already products inside the shopping cart, get the order object used to save the cart-related info from the Session system variable;
Obtain info of the product and create an instance of the OrderItemInfo(i.e. item) class to save this product-related info;
Search from inside the order objectsub items that contain the same product as that inside the item project, and update the sub items info;
Update the shopping cart.
The following lists the complete code for the ProductView_RowCommand function:
protected void ProductView_RowCommand(object
sender,GridViewCommandEventArgs e){
OrderItemInfo item = null;
OrderInfo order = null;
if(Session[Session.SessionID + OrderForm.Cart] == null) {
item = GetOrderItemInformation(e);
if(item == null) {
ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
"alert('Data Error!');", true);
return;
}
order = new OrderInfo();
order.OrderItemList.Add(item);
order.TotalMoney = item.Price;
order.TotalNumber = item.Number;
Session[Session.SessionID + OrderForm.Cart] = order;
}
else{
order = (OrderInfo)Session[Session.SessionID + OrderForm.Cart];
item = GetOrderItemInformation(e);
if(item == null) {
ScriptManager.RegisterStartupScript(Page, GetType(), "script2",
"alert('Data Error!');", true);
return;
}
int i = 0;
for(i = 0; i < order.OrderItemList.Count; i++){
if(item.ProductID == ((OrderItemInfo)order.OrderItemList[i]).ProductID) {
((OrderItemInfo)order.OrderItemList[i]).Number++;
((OrderItemInfo)order.OrderItemList[i]).ItemTotalMoney +=
item.ItemTotalMoney;
break;
}
}
if(i == order.OrderItemList.Count) {
order.OrderItemList.Add(item);
}
order.TotalNumber++;
order.TotalMoney += item.Price;
Session[Session.SessionID + OrderForm.Cart] = order;
}
ScriptManager.RegisterStartupScript(Page, GetType(), "script3",
"alert('You have succeeded in adding the selected goods into the shopping
cart!');", true);
}
For brevity, we omit the code listing for the GetOrderItemInformation helper function.
In addition, there are several issues that deserve to be discussed. First, because when we get the product information, the row index of the ProductView controlhas to be used, at the earlier event (RowCreated) of the ProductView controlwe put the index number of the product into the CommandArgument propertyof the buyBtn button(which is the IDproperty of the "Add to shopping cart" button). Here's the related code:
protected void ProductView_RowCreated(object sender,GridViewRowEventArgs
e){
if(e.Row.RowType == DataControlRowType.DataRow) {
Button buyBtn = (Button)e.Row.FindControl("BuyBtn");
if(buyBtn != null) {
buyBtn.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
Second, in traditional ASP.NET applications most of us like to use 'Response.Write("<script>window.alert('....')</script>");' to tell users the result of the current operation. However, due to leveraging the ASP.NET AJAX server control named UpdatePanelto enclose the GridView control we can not use that any more, or else we will meet some error like the one displayed in Figure 16.
Figure 16-an error occurs when we use 'Response.Write(...);' inside UpdatePanel control to give some clue to the users
/Building_ASP.NET_AJAX_Server-Centric_Shopping_Website(5)_html_m5070b3f2.png)
Therefore, we have to resort to a static function named RegisterStartupScriptof the ScriptManager control. Here is an example of what happens when the "adding" operation is successful.
ScriptManager.RegisterStartupScript(Page, GetType(), "script3",
"alert('You have succeeded in adding the selected goods into the shopping
cart!');", true);
Next: Viewing the Shopping Cart >>
More ASP.NET Articles
More By Xianzhong Zhu