hi I used below code to save number of selected Item in cookies:
protected void Imgorder_Click(object sender, EventArgs e)
{
ImageButton ibtn = sender as ImageButton;
RadioButton RB720 = ibtn.NamingContainer.FindControl("RB720") as RadioButton;
RadioButton RB1080 = ibtn.NamingContainer.FindControl("RB1080") as RadioButton;
int id = Convert.ToInt32((sender as ImageButton).CommandArgument);
DataTable dtFiles = GetFilmInfo(id);
string ImageName = dtFiles.Rows[0][22].ToString();
string Code = dtFiles.Rows[0][4].ToString();
string Daste = dtFiles.Rows[0][1].ToString();
string P720 = dtFiles.Rows[0][11].ToString();
string P1080 = dtFiles.Rows[0][13].ToString();
string F720 = dtFiles.Rows[0][192].ToString();
string F1080 = dtFiles.Rows[0][193].ToString();
string Price720 = dtFiles.Rows[0][185].ToString();
string Price1080 = dtFiles.Rows[0][186].ToString();
DataTable dt = new DataTable();
if (Session["Order"] != null)
{
dt = Session["Order"] as DataTable;
}
else
{
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id",typeof(int)),new DataColumn("ImageName",typeof(string)),new DataColumn("SeP",typeof(decimal)),
new DataColumn("code",typeof(string)),new DataColumn("Daste",typeof(string)),new DataColumn("Quality",typeof(string)),new DataColumn("format",typeof(string)),new DataColumn("Se",typeof(string))
,new DataColumn("Quantity", typeof(int))});
}
if (RB720.Checked || RB1080.Checked || RBT3D.Checked || RBRip.Checked || RBDvd.Checked)
{
if (RB720.Checked)
{
dt.Rows.Add(dt.Rows.Count + 1, ImageName, Price720, Code, Daste, "720P", "فرمت: " + F720, " ", "1");
}
if (RB1080.Checked)
{
dt.Rows.Add(dt.Rows.Count + 1, ImageName, Price1080, Code, Daste, "1080P", "فرمت: " + F1080, " ", "1");
}
}
int totalOrder = dt.AsEnumerable().Sum(row => row.Field<int>("Quantity"));
Lblorder.Text = totalOrder.ToString();
Response.Cookies["UserData"].Value = Lblorder.Text.Trim();
Session["totalorder"] = Lblorder.Text;
Session["Order"] = dt;
}
in below code it will save number of row in cookies:
int totalOrder = dt.AsEnumerable().Sum(row => row.Field<int>("Quantity"));
Lblorder.Text = totalOrder.ToString();
Response.Cookies["UserData"].Value = Lblorder.Text.Trim();
in order.aspx page I use gridview to show session's data on it...
and in Lblorder will show cookies["UserData"]
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["UserName"] != null)
{
Lblorder.Text = Request.Cookies["UserData"].Value;
}
else {
Lblorder.Text = "0";
Lblorder.ToolTip = "سبد خرید شما خالی می باشد.";
}
if (!this.IsPostBack)
{
BindData();
}
}
and:
private void BindData()
{
if (Session["Order"] != null)
{
DataTable dt = Session["Order"] as DataTable;
gvOrders.DataSource = dt;
gvOrders.DataBind();
if (dt.Rows.Count > 0)
{
decimal totalPrice = dt.AsEnumerable().Sum(row => row.Field<decimal>("SeP"));
int total = Convert.ToInt32(totalPrice);
lbltotal.Text = total.ToString("N0") ;
Session["Total"] = total.ToString();
}
}
}
and in grid view I put remove button to delete data from grid view:
protected void Remove(object sender, EventArgs e)
{
if (Session["Order"] != null)
{
DataTable dtOrder = Session["Order"] as DataTable;
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
int id = Convert.ToInt32(gvOrders.DataKeys[row.RowIndex].Value);
DataRow orderRow = dtOrder.Select().Single(x => Convert.ToInt32(x["Id"]) == (id));
dtOrder.Rows.Remove(orderRow);
dtOrder.AcceptChanges();
Session["Order"] = dtOrder;
BindData();
}
}
in above code it will delete one row from gridview...
now I want when I click on remove button when it delete row from gridview it decrease number of cookies ...
i.e:
value of cookie["userdata"]=4 when I delete row from grid view it will be cookie["UserData"]=3
how I can do it?
Best Regards
Neda