WebApplication與Profile購物車

WebApplication中使用Profile做購物車功能。開發環境VS.NET 2008

Web.config文件配置

  1. <system.web>
  2.     <anonymousIdentification enabled="true"/>
  3.     <profile inherits="ShoppingCartTest.UserProfile">
  4.     </profile>
  5. </system.web>

UserProfile

  1. using System.Web.Profile;
  2. using System.Web.Security;
  3. namespace ShoppingCartTest
  4. {
  5.     public class UserProfile : ProfileBase
  6.     {
  7.         public static UserProfile GetUserProfile(string username)
  8.         {
  9.             return ((UserProfile)(ProfileBase.Create(username)));
  10.         }
  11.         public static UserProfile GetUserProfile()
  12.         {
  13.             string userName = System.Web.HttpContext.Current.User.Identity.Name;
  14.             return Create(userName) as UserProfile;
  15.         }
  16.         public virtual ProfileShoppingCart ProfileShoppingCart
  17.         {
  18.             get
  19.             {
  20.                 return ((ProfileShoppingCart)(this.GetPropertyValue("ProfileShoppingCart")));
  21.             }
  22.             set
  23.             {
  24.                 this.SetPropertyValue("ProfileShoppingCart", value);
  25.             }
  26.         }
  27.     }
  28. }

 

ProfileShoppingCart

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. namespace ShoppingCartTest
  7. {
  8.     [Serializable]
  9.     public class ProfileShoppingCart
  10.     {
  11.         public DataTable _CartItems = new DataTable("CartItems");
  12.         public ProfileShoppingCart()
  13.         {
  14.             CreatTable();
  15.         }
  16.         // 返回購物車中所有的商品
  17.         public DataTable CartItems
  18.         {
  19.             get { return _CartItems; }
  20.         }
  21.         // 計算購物車中所有商品的總價錢
  22.         public decimal Total
  23.         {
  24.             get
  25.             {
  26.                 decimal sum = 0;
  27.                 foreach (DataRow row in _CartItems.Rows)
  28.                    sum += Convert.ToDecimal(row["Price"].ToString()) * Convert.ToDecimal(row["Quantity"].ToString());
  29.                 return sum;
  30.             }
  31.         }
  32.         // 添加商品到購物車
  33.         public void AddItem(int ID, string Name, decimal Price)
  34.         {
  35.             if (_CartItems.Select("ID=" + ID).Length == 0)
  36.             {
  37.                 DataRow row = _CartItems.NewRow();
  38.                 row["ID"] = ID;
  39.                 row["Name"] = Name;
  40.                 row["Price"] = Price;
  41.                 row["Quantity"] = 1;
  42.                 _CartItems.Rows.Add(row);
  43.             }
  44.             else
  45.             {
  46.                 foreach (DataRow row in _CartItems.Select("ID=" + ID))
  47.                 {
  48.                     row["Quantity"] = Convert.ToDecimal(row["Quantity"].ToString()) + 1;
  49.                 }
  50.             }
  51.         }
  52.         // 移除購物車中的商品
  53.         public void RemoveItem(int ID)
  54.         {
  55.             if (_CartItems.Select("ID=" + ID).Length == 0)
  56.             {
  57.                 return;
  58.             }
  59.             else
  60.             {
  61.                 foreach (DataRow row in _CartItems.Select("ID=" + ID))
  62.                 {
  63.                     row["Quantity"] = Convert.ToDecimal(row["Quantity"].ToString()) - 1;
  64.                     if (Convert.ToDecimal(row["Quantity"].ToString()) == 0)
  65.                     {
  66.                         _CartItems.Rows.Remove(row);
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.         // 創建DataTable
  72.         private void CreatTable()
  73.         {
  74.             _CartItems.Columns.Add("ID"typeof(int));
  75.             _CartItems.Columns.Add("Name",typeof(string));
  76.             _CartItems.Columns.Add("Price",typeof(decimal));
  77.             _CartItems.Columns.Add("Quantity"typeof(decimal));
  78.         }
  79.     }
  80. }

 

shopping.aspx代碼

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="shopping.aspx.cs" Inherits="ShoppingCartTest.shopping" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <table width="100%">
  10.         <tr>
  11.             <td valign="top">
  12.                 <h2>
  13.                     Products</h2>
  14.                 <asp:GridView ID="ProductGrid" DataSourceID="ProductSource" DataKeyNames="ProductID"
  15.                     AutoGenerateColumns="false" OnSelectedIndexChanged="AddCartItem" ShowHeader="false"
  16.                     CellPadding="5" runat="Server">
  17.                     <Columns>
  18.                         <asp:ButtonField CommandName="select" Text="Buy" />
  19.                         <asp:BoundField DataField="ProductName" />
  20.                         <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" />
  21.                     </Columns>
  22.                 </asp:GridView>
  23.                 <asp:SqlDataSource ID="ProductSource" ConnectionString="Server=localhost;Database=Northwind;Trusted_Connection=true;"
  24.                     SelectCommand="SELECT ProductID,ProductName,UnitPrice FROM Products" runat="Server" />
  25.             </td>
  26.             <td valign="top">
  27.                 <h2>
  28.                     Shopping Cart</h2>
  29.                 <asp:GridView ID="CartGrid" AutoGenerateColumns="false" DataKeyNames="ID" OnSelectedIndexChanged="RemoveCartItem"
  30.                     CellPadding="5" Width="300" runat="Server">
  31.                     <Columns>
  32.                         <asp:ButtonField CommandName="select" Text="Remove" />
  33.                         <asp:BoundField DataField="Name" HeaderText="Name" />
  34.                         <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:c}" />
  35.                         <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
  36.                     </Columns>
  37.                 </asp:GridView>
  38.                 <b>Total:</b>
  39.                 <asp:Label ID="lblTotal" runat="Server" />
  40.                 <br />
  41.                 <b> Profile:</b><asp:Label ID="lblProfile" runat="server" Text="[lblProfile]"></asp:Label>
  42.                 <br />
  43.                 <a href="profileView.aspx" target="_blank">profileView</a>
  44.                 <br />
  45.     <asp:Button ID="btnClearProfile" runat="server" οnclick="btnClearProfile_Click" 
  46.         Text="ClearProfile" />
  47.                 <br />
  48.     <asp:GridView ID="GridView1" runat="server">
  49.     </asp:GridView>
  50.             </td>
  51.         </tr>
  52.     </table>
  53.     </form>
  54. </body>
  55. </html>

 

shopping.aspx.cs代碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Globalization;
  8. using System.Web.Profile;
  9. namespace ShoppingCartTest
  10. {
  11.     public partial class shopping : System.Web.UI.Page
  12.     {
  13.         private static UserProfile Profile ;
  14.         // 綁定購物車
  15.         private void BindShoppingCart()
  16.         {
  17.             if (Profile.ProfileShoppingCart != null)
  18.             {
  19.                 CartGrid.DataSource = Profile.ProfileShoppingCart.CartItems;
  20.                 CartGrid.DataBind();
  21.                 lblTotal.Text = Profile.ProfileShoppingCart.Total.ToString("c");
  22.             }
  23.         }
  24.         protected void Page_Load(object sender, EventArgs e)
  25.         {
  26.             if (!IsPostBack)
  27.             {
  28.                 //Profile = UserProfile.GetUserProfile("ProfileShoppingCart");
  29.                 Profile = UserProfile.GetUserProfile();
  30.                 
  31.                 BindShoppingCart();
  32.             }
  33.             GridView1.DataSource = ProfileManager.GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption.All);
  34.             GridView1.DataBind();
  35.             lblProfile.Text = Profile.UserName;
  36.         }
  37.         // 移除購物車中的產品
  38.         protected void RemoveCartItem(object sender, EventArgs e)
  39.         {
  40.             int ID = (int)CartGrid.SelectedDataKey.Value;
  41.             Profile.ProfileShoppingCart.RemoveItem(ID);
  42.             Profile.Save();
  43.             BindShoppingCart();
  44.         }
  45.         // 添加產品到購物車
  46.         protected void AddCartItem(object sender, EventArgs e)
  47.         {
  48.             GridViewRow row = ProductGrid.SelectedRow;
  49.             int ID = (int)ProductGrid.SelectedDataKey.Value;
  50.             String Name = row.Cells[1].Text;
  51.             decimal Price = Decimal.Parse(row.Cells[2].Text,
  52.               NumberStyles.Currency);
  53.             if (Profile.ProfileShoppingCart == null)
  54.                 Profile.ProfileShoppingCart = new ProfileShoppingCart();
  55.             Profile.ProfileShoppingCart.AddItem(ID, Name, Price);
  56.             Profile.Save();
  57.             BindShoppingCart();
  58.         }
  59.         protected void btnClearProfile_Click(object sender, EventArgs e)
  60.         {
  61.             foreach (ProfileInfo pf in ProfileManager.GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption.All))
  62.             {
  63.                 if (DateTime.Now.Day - pf.LastUpdatedDate.Day >= 1)
  64.                 {
  65.                     ProfileManager.DeleteProfile(pf.UserName);
  66.                 }
  67.             }
  68.             GridView1.DataSource = ProfileManager.GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption.All);
  69.             GridView1.DataBind();
  70.         }
  71.     }
  72. }

 

Global.asax.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.SessionState;
  7. using System.Web.Profile;
  8. namespace ShoppingCartTest
  9. {
  10.     public class Global : System.Web.HttpApplication
  11.     {
  12.         protected void Profile_MigrateAnonymous(Object sender,ProfileMigrateEventArgs e)
  13.         {
  14.             UserProfile Profile = UserProfile.GetUserProfile("ProfileShoppingCart");
  15.             UserProfile anonProfile = UserProfile.GetUserProfile(e.AnonymousID);
  16.             Profile.ProfileShoppingCart = anonProfile.ProfileShoppingCart;
  17.         }  
  18.     }
  19. }

 

轉至:http://www.cnblogs.com/mimengjiangnan/archive/2008/12/10/1351968.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章