Can

csharp , xhtml Linz, Austria
  • 13 years ago

     

  • 12 years ago

     The Silver Spoon:

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="TestBed.WebGui.TestForm" debug="true" ContentType="text/html" ResponseEncoding="iso-8859-1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

        <head>
            <title>Shopping Cart</title>

            <script runat="server">        
                private System.Data.DataTable objDT;
                private System.Data.DataRow objDR;
            
                private void Page_Load(object s, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        makeCart();
                    }
                }
            
                void makeCart()
                {
                    objDT = new System.Data.DataTable("Cart");
                    objDT.Columns.Add("ID", typeof(int));

                    objDT.Columns["ID"].AutoIncrement = true;
                    objDT.Columns["ID"].AutoIncrementSeed = 1;

                    objDT.Columns.Add("Quantity", typeof(int));
                    objDT.Columns.Add("Product", typeof(string));

                    objDT.Columns.Add("Cost", typeof(Decimal));
                    Session["Cart"] = objDT;
                }

            
                void AddToCart(object s, EventArgs e)
                {
                    objDT = (System.Data.DataTable) Session["Cart"];
                    object Product = ddlProducts.SelectedItem.Text;
                    bool blnMatch = false;
                   
                    foreach (System.Data.DataRow objDR in objDT.Rows)
                    {

                        if ((objDR["Product"] == Product))
                        {
                            objDR["Quantity"] = (objDR["Quantity"] + txtQuantity.Text);

                            blnMatch = true;
                            break;
                        }
                    }
                   
                    if (!blnMatch)
                    {
                        objDR = objDT.NewRow();
                        objDR["Quantity"] = txtQuantity.Text;

                        objDR["Product"] = ddlProducts.SelectedItem.Text;
                        objDR["Cost"] = Decimal.Parse(ddlProducts.SelectedItem.Value);

                        objDT.Rows.Add(objDR);
                    }
                   
                    Session["Cart"] = objDT;
                    dg.DataSource = objDT;

                    dg.DataBind();
                    lblTotal.Text = ("$" + GetItemTotal());
                }

            
                Decimal GetItemTotal()
                {
                    Decimal decRunningTotal = 0;
                   
                    for (int intCounter = 0; (intCounter<= (objDT.Rows.Count - 1)); intCounter++)
                    {
                        objDR = objDT.Rows[intCounter];
                        decRunningTotal = (decRunningTotal + ((decimal)objDR["Cost"] * (int)objDR["Quantity"]));
                    }
                   
                    return decRunningTotal;
                }       

                void Delete_Item(object s, DataGridCommandEventArgs e)
                {
                    objDT = (System.Data.DataTable)Session["Cart"];
                    objDT.Rows[e.Item.ItemIndex].Delete();
                    Session["Cart"] = objDT;
                    dg.DataSource = objDT;
                    dg.DataBind();

                    lblTotal.Text = ("$" + GetItemTotal());
                }
            </script>
        </head>
       
        <body>
            <form id="Form1" runat="server">
            Product:<br/>
                <asp:DropDownList id="ddlProducts" runat="server">
                    <asp:ListItem Value="4.99">Socks</asp:ListItem>

                    <asp:ListItem Value="34.99">Pants</asp:ListItem>
                    <asp:ListItem Value="14.99">Shirt</asp:ListItem>

                    <asp:ListItem Value="12.99">Hat</asp:ListItem>

                </asp:DropDownList><br/>
               
            Quantity:<br/>
                <asp:textbox id="txtQuantity" runat="server" /><br/><br/>
                <asp:Button id="btnAdd" runat="server" Text="Add To Cart" onClick="AddToCart" /><br/><br/>

                <asp:DataGrid id="dg" runat="server" ondeletecommand="Delete_Item">
                    <columns>
                        <asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove Item" />
                    </columns>
                </asp:DataGrid>

            <br/><br/>

            Total:
                <asp:Label id="lblTotal" runat="server" />

            </form>
        </body>

    </html>
     

    Joe 

  • 12 years ago
    You're the best JOE!!!!, thanks a million!!!!, finally it works!!! God bless you!!!!! Is there a way I can give you credit on this site??
  • 12 years ago

    If you can see yourself helping others out in similar circumstances in the future, then that's credit enough. Glad it helped you out.

    Joe 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne