我想要做的是当用户点击按钮(“添加到购物车”)时,它会改变颜色并更改为(“添加到购物车”),我设法做到了。
我的查询是当用户再次点击“添加到购物车”按钮时,突出显示将关闭并返回(“添加到购物车”)。一切正常,但是,当我尝试从突出显示中删除所有产品时,一个产品会保持突出显示。例如,我添加了4个产品,当我决定删除它们时,一个产品保持突出显示并具有“添加到购物车”文本。
这是按钮点击的代码:
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;
}
}
}
和unhighlight方法:
{{1}}
请帮帮我,我真的需要做这个工作。谢谢你的承认。
答案 0 :(得分:0)
很抱歉在这里问一下,但是你在哪里使用RemoveHighLightCartProducts?你是否在从购物车中取出行之前尝试过?