hi.
i am trying to do a shopping cart project where whenever u add some product to the cart i will store in a session array with 3 variables. its a spectacle shop so.3 things are to be added into array is pid,total no of amount and lens type.bt that's not the big issue.
I have done the part to add the product details into an array. and showing the array in a dynamically created array. here in the table whenever its showing the details its created a delete button. now i have stuck to delete the product details from session whenever user click a particular button to delete a product from cart.
webapplication1 --->> here i am adding the details in 3 textboxes. and in webapplication2 its showing the array along with the delete button..
and the array is created in the cart.cs
webapplication1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication25.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm2.aspx">View table</asp:HyperLink>
</div>
</form>
</body>
</html
>
webapplication1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication_Cart;
namespace WebApplication25
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Cart_array c = new Cart_array();
Cart obj = new Cart();
obj.pid = TextBox1.Text;
obj.amount = Convert.ToInt32(TextBox2.Text);
obj.lens_type = TextBox3.Text;
if (Session["cart"] == null)
{
c.add(obj);
Session["cart"] = c;
}
if (Session["cart"] != null)
{
c = (WebApplication_Cart.Cart_array)Session["cart"];
c.add(obj);
}
}
}
}
webapplication2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication25.WebForm2" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<asp:Table ID="tblName" runat="server" BorderWidth="1px">
</asp:Table>
<br />
</div>
</form>
</body>
</html
>
webapplication2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication_Cart;
namespace WebApplication25
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["cart"] != null)
{
WebApplication_Cart.Cart_array obj1 = new WebApplication_Cart.Cart_array();
obj1=(WebApplication_Cart.Cart_array) Session["cart"];
for (int i = 0; i < obj1.len; i++)
{
TableRow t = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TableCell c3 = new TableCell();
TableCell tcButton = new TableCell();
Button btn = new Button();
c1.Text = obj1.obj[i].pid.ToString();
c2.Text = obj1.obj[i].amount.ToString();
c3.Text = obj1.obj[i].lens_type.ToString();
btn.Text = "delete";
tcButton.Controls.Add(btn);
t.Cells.Add(c1);
t.Cells.Add(c2);
t.Cells.Add(c3);
t.Cells.Add(tcButton);
tblName.Rows.Add(t);
//Label1.Text = obj1.obj[i].pid.ToString();
Label1.Text += obj1.obj[i].pid.ToString() +obj1.obj[i].amount.ToString() + obj1.obj[i].lens_type.ToString();
}
}
}
}
}
cart.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication_Cart
{
public class Cart
{
public string pid,lens_type;
public int amount;
private int p;
}
public class Cart_array
{
public Cart[] obj;
public int len;
public Cart_array()
{
len = 0;
obj = new Cart[10];
for (int i = 0; i < 10; i++)
{
obj[i] = new Cart();
}
}
public void add(Cart e)
{
obj[len].pid = e.pid;
obj[len].amount = e.amount;
obj[len].lens_type = e.lens_type;
len++;
}
}
}
pls help me out..
thank u