再次点击按钮后如何移除所选产品
本文关键字:何移 按钮 | 更新日期: 2023-09-27 18:01:39
我有一个购物车,当我点击按钮["Add to cart "]时,它高亮显示为蓝色,并将其名称更改为["Add to cart "]。然而,我希望它是不突出显示,并更改名称回到"添加到购物车"。代码很长,但我希望你们能帮助我。这是我的添加到购物车按钮代码:
protected void btnAddToCart_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductQuantity = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyCart"] != null)
{
DataTable dt = (DataTable)Session["MyCart"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID); // check whether product is already added or not
if (checkProduct.Count() == 0)
{
string query = "select * from Products where ProductID = " + ProductID + "";
DataTable dtProducts = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["Name"] = Convert.ToString(dtProducts.Rows[0]["Name"]);
dr["Description"] = Convert.ToString(dtProducts.Rows[0]["Description"]);
dr["Price"] = Convert.ToString(dtProducts.Rows[0]["Price"]);
dr["ImageUrl"] = Convert.ToString(dtProducts.Rows[0]["ImageUrl"]);
dr["ProductQuantity"] = ProductQuantity;
dr["AvailableStock"] = lblAvailableStock.Text;
dt.Rows.Add(dr);
Session["MyCart"] = dt;
btnIslandGas.Text = dt.Rows.Count.ToString();
}
else
{
if (checkProduct.Count() != 0)
{
var ProductRowToBeDeleted = dt.Select("ProductID =" + ProductID);
foreach (var row in ProductRowToBeDeleted)
{
row.Delete();
btnIslandGas.Text = dt.Rows.Count.ToString();
}
}
}
}
else
{
string query = "select * from Products where ProductID = " + ProductID + "";
DataTable dtProducts = GetData(query);
DataTable dt = new DataTable(); //storing all of the records
dt.Columns.Add("ProductID", typeof(string)); // adding the columns
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Price", typeof(string));
dt.Columns.Add("ImageUrl", typeof(string));
dt.Columns.Add("ProductQuantity", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
DataRow dr = dt.NewRow(); //adding the rows
dr["ProductID"] = ProductID;
dr["Name"] = Convert.ToString(dtProducts.Rows[0]["Name"]);
dr["Description"] = Convert.ToString(dtProducts.Rows[0]["Description"]);
dr["Price"] = Convert.ToString(dtProducts.Rows[0]["Price"]);
dr["ImageUrl"] = Convert.ToString(dtProducts.Rows[0]["ImageUrl"]);
dr["ProductQuantity"] = ProductQuantity;
dr["AvailableStock"] = lblAvailableStock.Text;
dt.Rows.Add(dr); //adding the data row in the data table.
Session["MyCart"] = dt; //asigning the datatable in the session.
btnIslandGas.Text = dt.Rows.Count.ToString();
}
HighLightCartProducts();
}
编辑后突出显示购物车代码
private void HighLightCartProducts()
{
if (Session["MyCart"] != null)
{
DataTable dtProductsAddedToCart = (DataTable)Session["MyCart"];
if (dtProductsAddedToCart.Rows.Count > 0)
{
foreach (DataListItem item in dlProducts.Items)
{
HiddenField hfProductID = item.FindControl("hfProductID") as HiddenField; // Getting hidden filed value
if (dtProductsAddedToCart.AsEnumerable().Any(row => hfProductID.Value == row.Field<String>("ProductID")))
{
//item.BackColor = System.Drawing.Color.Red;
Button btnAddToCart = item.FindControl("btnAddToCart") as Button; //item.FinControl finds the item(Button)
btnAddToCart.BackColor = System.Drawing.Color.Blue;
btnAddToCart.ForeColor = System.Drawing.Color.White;
btnAddToCart.Text = "Added to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = true;
}
else
{
Button btnAddToCart = item.FindControl("btnAddToCart") as Button;
btnAddToCart.BackColor = System.Drawing.Color.White;
btnAddToCart.ForeColor = System.Drawing.Color.Black;
btnAddToCart.Text = "Add to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = false;
}
}
}
}
}
和添加删除高亮代码
private void RemoveHighLightCartProducts(string ProductId)
{
if (Session["MyCart"] != null)
{
DataTable dtProductsAddedToCart = (DataTable)Session["MyCart"];
//delete row which contains product data.
var ProductRowToBeDeleted = dtProductsAddedToCart.Select("ProductID =" + ProductId);
foreach (var row in ProductRowToBeDeleted)
{
row.Delete();
}
foreach (DataListItem item in dlProducts.Items)
{
Button btnAddToCart = item.FindControl("btnAddToCart") as Button;
btnAddToCart.BackColor = System.Drawing.Color.Red;
btnAddToCart.ForeColor = System.Drawing.Color.White;
btnAddToCart.Text = "Add to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = false;
}
}
}
只需在代码中添加此代码,它检查产品是否已添加到购物车中,然后从购物车中删除该产品并删除高亮显示。我建议分别编写高亮显示和删除高亮显示的方法。
protected void btnAddToCart_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductQuantity = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyCart"] != null)
{
DataTable dt = (DataTable)Session["MyCart"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID); // check whether product is already added or not
if (checkProduct.Count() == 0)
{
string query = "select * from Products where ProductID = " + ProductID + "";
DataTable dtProducts = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["Name"] = Convert.ToString(dtProducts.Rows[0]["Name"]);
dr["Description"] = Convert.ToString(dtProducts.Rows[0]["Description"]);
dr["Price"] = Convert.ToString(dtProducts.Rows[0]["Price"]);
dr["ImageUrl"] = Convert.ToString(dtProducts.Rows[0]["ImageUrl"]);
dr["ProductQuantity"] = ProductQuantity;
dr["AvailableStock"] = lblAvailableStock.Text;
dt.Rows.Add(dr);
Session["MyCart"] = dt;
btnIslandGas.Text = dt.Rows.Count.ToString();
HighLightCartProducts();
}
else
{
RemoveHighLightCartProducts(ProductId);
}
}
else
{
string query = "select * from Products where ProductID = " + ProductID + "";
DataTable dtProducts = GetData(query);
DataTable dt = new DataTable(); //storing all of the records
dt.Columns.Add("ProductID", typeof(string)); // adding the columns
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Price", typeof(string));
dt.Columns.Add("ImageUrl", typeof(string));
dt.Columns.Add("ProductQuantity", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
DataRow dr = dt.NewRow(); //adding the rows
dr["ProductID"] = ProductID;
dr["Name"] = Convert.ToString(dtProducts.Rows[0]["Name"]);
dr["Description"] = Convert.ToString(dtProducts.Rows[0]["Description"]);
dr["Price"] = Convert.ToString(dtProducts.Rows[0]["Price"]);
dr["ImageUrl"] = Convert.ToString(dtProducts.Rows[0]["ImageUrl"]);
dr["ProductQuantity"] = ProductQuantity;
dr["AvailableStock"] = lblAvailableStock.Text;
dt.Rows.Add(dr); //adding the data row in the data table.
Session["MyCart"] = dt; //asigning the datatable in the session.
btnIslandGas.Text = dt.Rows.Count.ToString();
HighLightCartProducts();
}
}
private void HighLightCartProducts()
{
if (Session["MyCart"] != null)
{
DataTable dtProductsAddedToCart = (DataTable)Session["MyCart"];
if (dtProductsAddedToCart.Rows.Count > 0)
{
foreach (DataListItem item in dlProducts.Items)
{
HiddenField hfProductID = item.FindControl("hfProductID") as HiddenField; // Getting hidden filed value
if (dtProductsAddedToCart.AsEnumerable().Any(row => hfProductID.Value == row.Field<String>("ProductID")))
{
//item.BackColor = System.Drawing.Color.Red;
Button btnAddToCart = item.FindControl("btnAddToCart") as Button; //item.FinControl finds the item(Button)
btnAddToCart.BackColor = System.Drawing.Color.Blue;
btnAddToCart.ForeColor = System.Drawing.Color.White;
btnAddToCart.Text = "Added to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = true;
}
}
}
}
}
private void RemoveHighLightCartProducts(string ProductId)
{
if (Session["MyCart"] != null)
{
DataTable dtProductsAddedToCart = (DataTable)Session["MyCart"];
//delete row which contains product data.
var ProductRowToBeDeleted = dtProductsAddedToCart.Select("ProductID =" + ProductId);
foreach (var row in ProductRowToBeDeleted)
{
row.Delete();
}
foreach (DataListItem item in dlProducts.Items)
{
//if (dtProductsAddedToCart.AsEnumerable().Any(row => ProductID!=row.Field<String>("ProductID")))
foreach((DataRow rw in dtProductsAddedToCart.Rows)
{
if(rw["ProductID"])!=ProductID)
{
Button btnAddToCart = item.FindControl("btnAddToCart") as Button; //item.FinControl finds the item(Button)
btnAddToCart.BackColor = System.Drawing.Color.Blue;
btnAddToCart.ForeColor = System.Drawing.Color.White;
btnAddToCart.Text = "Added to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = true;
}
else
{
Button btnAddToCart = item.FindControl("btnAddToCart") as Button;
btnAddToCart.BackColor = System.Drawing.Color.Red;
btnAddToCart.ForeColor = System.Drawing.Color.White;
btnAddToCart.Text = "Add to Cart";
Image imgGreenstar = item.FindControl("imgStar") as Image;
imgGreenstar.Visible = false;
}
}
}
}
}