⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcart.aspx

📁 东软内部材料(四)asp等相关的教学案例 
💻 ASPX
字号:
<%@ Page Language="C#" Inherits="IBuyAdventure.PageBase" src="components/stdpage.cs" clienttarget=downlevel %>
<%@ Register TagPrefix="IBA" TagName="Header" Src="UserControl\Header.ascx" %>
<%@ Register TagPrefix="IBA" TagName="Footer" Src="UserControl\Footer.ascx" %>
<%@ Register TagPrefix="IBA" TagName="Categories" Src="UserControl\Categories.ascx" %>
<%@ Import Namespace="System.Data" %>

<html>
  <head>
    <title>IBuyAdventure Catalog</title>
  </head>

  <script language="C#" runat="server" >
		// Total for shopping basket
		double fTotal = 0;

		void Page_Load(Object sender, EventArgs e) {
			IBuyAdventure.CartDB cart = new IBuyAdventure.CartDB(getConnStr());
			// If page is not being loaded in response to postback
			if (Page.IsPostBack == false) {
				// If a new product to add is specified, add it to the shopping cart
				if (Request.Params["ProductCode"] != null) {
					cart.AddShoppingCartItem(GetCustomerID(), Request.Params["ProductCode"]);
				}
				PopulateShoppingCartList();
				UpdateSelectedItemStatus();
			}
		}

		void UpdateSelectedItemStatus() {
			if  ( MyList.Items != null ) {
				status.Text = "You have selected " + MyList.Items.Count +" items " + "so far:<br>";
			}
			else {
				status.Text = "You have selected no items so far";
			}
		}

		void Recalculate_Click(Object sender, EventArgs e) {
			// Update Shopping Cart
			UpdateShoppingCartDatabase();
			// Repopulate ShoppingCart List
			PopulateShoppingCartList();
			UpdateSelectedItemStatus();
		}

		void Checkout_Click(Object sender, EventArgs e) {
			// Update Shopping Cart
			UpdateShoppingCartDatabase();
			Response.Redirect("secure/Checkout.aspx");
		}

		String GetCustomerID() {
			if (User.Identity.Name != "") {
				return Context.User.Identity.Name;
			}
			else {
				if (Session["AnonUID"] == null)
					Session["AnonUID"] = Guid.NewGuid();
				return Session["AnonUID"].ToString();
			}
		}

		void UpdateShoppingCartDatabase() {
			IBuyAdventure.ProductsDB inventory = new IBuyAdventure.ProductsDB(getConnStr());
			IBuyAdventure.CartDB cart = new IBuyAdventure.CartDB(getConnStr());
			// Iterate through all rows within shopping cart list
			for (int i=0; i<MyList.Items.Count; i++) {
				// Obtain references to row's controls
				TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");
				CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");
				HtmlInputHidden shoppingCartIDTxt = (HtmlInputHidden) MyList.Items[i].FindControl("ShoppingCartID");
				// If removed checkbox selected, delete the item.  Otherwise, update its
				// quantity (todo: a product quality app should only update if value was been changed)
				// Convert quantity to a number
				int Quantity = Int32.Parse(quantityTxt.Text);
				if (remove.Checked == true || Quantity  == 0)
					cart.DeleteShoppingCartItem(Int32.Parse(shoppingCartIDTxt.Value));
				else
					cart.UpdateShoppingCartItem(Int32.Parse(shoppingCartIDTxt.Value), Quantity );
			}
		}

		void PopulateShoppingCartList() {
			// Popoulate list with updated shopping cart data
			IBuyAdventure.CartDB cart = new IBuyAdventure.CartDB(getConnStr());
			DataSet ds = cart.GetShoppingCartItems(GetCustomerID());
			MyList.DataSource = ds;
			MyList.DataBind();
			// Walk through table and calculate the total value
			DataTable dt = ds.Tables[0];
			int lIndex, Quantity;
			double UnitPrice;
			for ( lIndex =0; lIndex < dt.Rows.Count; lIndex++ ) {
				UnitPrice = (double) dt.Rows[lIndex]["UnitPrice"];
				Quantity = (int) dt.Rows[lIndex]["Quantity"];
				if ( Quantity > 0 )
					fTotal += UnitPrice * Quantity;
			}
		}
  </script>

  <body background="images/back_sub.gif">
    <form runat="server">
      <font face="Verdana, Arial, Helvetica" size="2">
        <table border="0" valign="top" width="100%" style="font: 10pt verdana">
          <tr>
            <td colspan="5">
              <IBA:Header id="Header" runat="server"/>
            </td>
          </tr>
          <!--Begin Navigational Buttons-->
          <tr valign="top">
            <td width="95" align="left" valign="top">
              <IBA:Categories id="Categories" runat="server"/>
            </td>
            <td>
              &nbsp;&nbsp;
            </td>
            <td valign="top" align="left">
              <img src="images/hd_Check_out.gif">
              <br><br>
              <font size="2"><asp:label id="status" runat="server" /></font><br>
              <table colspan="8" cellpadding="5" border="0" valign="top">
                <!-- BEGIN column header row -->
                <tr valign="top">
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Remove</font>
                  </td>
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Product Code</font>
                  </td>
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Product Name</font>
                  </td>
                  <td align="center" width="250" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Description</font>
                  </td>
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Quantity</font>
                  </td>
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Unit Price</font>
                  </td>
                  <td align="center" bgcolor="#800000">
                    <font style="Verdana, Arial, Helvetica" color="#ffffff" size="2">Unit Total</font>
                  </td>
                </tr>
                <asp:repeater id="MyList" runat="server">
                  <itemtemplate>
                    <tr>
                      <td align="center" bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <asp:checkbox id="Remove" runat="server"/>
                        </font>
                      </td>
                      <td align="center" bgcolor="#f7efde">
                        <input id="ShoppingCartID" type="hidden" value='<%#DataBinder.Eval(Container.DataItem, "ShoppingCartID", "{0:g}") %>' runat="server">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <%#DataBinder.Eval(Container.DataItem, "ProductCode")%>
                        </font>
                      </td>
                      <td align="center" bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <%#DataBinder.Eval(Container.DataItem, "ProductName")%>
                        </font>
                      </td>
                      <td align="left" width=250 bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <%#DataBinder.Eval(Container.DataItem, "Description")%>
                        </font>
                      </td>
                      <td align="center" bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <asp:textbox id="Quantity" text=<%#DataBinder.Eval(Container.DataItem, "Quantity", "{0:g}")%> width=30 runat="server"/>
                        </font>
                      </td>
                      <td align="center" bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <asp:label id="UnitPrice" runat="server"><%#DataBinder.Eval(Container.DataItem, "UnitPrice", "{0:C}")%></asp:label>
                        </font>
                      </td>
                      <td align="center" bgcolor="#f7efde">
                        <font style="Verdana, Arial, Helvetica" color="black" size="2">
                          <%# String.Format("{0:C}", ( ((int)DataBinder.Eval(Container.DataItem, "Quantity")) * ((double) DataBinder.Eval(Container.DataItem, "UnitPrice")) )) %>
                        </font>
                      </td>
                    </tr>
                  </itemtemplate>
                </asp:repeater>
                <tr>
                  <td colspan="5"><td colspan="3" align="right">
                    <font style="Verdana, Arial, Helvetica" color="black" size="2">
                      Total is <%=String.Format("{0:C}", fTotal ) %>
                    </font>
                  </td>
                </tr>
                <tr>
                  <td colspan="8" align="right">
                    <asp:button text="Recalculate" OnClick="Recalculate_Click" runat="server"/>
                    <asp:button text="Go To Checkout" OnClick="Checkout_Click" runat="server"/>
                  </td>
                </tr>
              </table>
             <IBA:footer runat="server"/></td> </tr>
            </td>
          </tr>
        </table>
      </font>
    </form>
  </body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -