我的购物车标签值在主页中没有增加?

时间:2016-05-20 05:58:20

标签: c# asp.net

MasterPage代码:

 public Label OnlbCartCountMasterPage {
        get { return this.chartlabel; }
    }

Index.aspx代码

String cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
string cartQuantity;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadList();
    }

    if (Session["addtocart"] != null)
    {
        DataTable dt = new DataTable();
        cartQuantity = Convert.ToString(dt.Rows.Count);

    }
    else
    {

        cartQuantity = "0";
    }
    Master.OnlbCartCountMasterPage.Text = cartQuantity;
}

Add2cart方法:

private void add2cart(int id,string lblname,int lblPrice, int lbltotal, int quantitylist, string image) 
{

    if (Session["addtocart"] != null)
    {
        DataTable dt = (DataTable)Session["addtocart"];
        var dataRow = dt.AsEnumerable().Where(x => x.Field<int>("ID") == id);
        if (dataRow.Count() == 0)
        {
            //lblErrorMessage.Text = "";
            DataRow dr = dt.NewRow();
            dr["ID"] = id;
            dr["Item"] = lblname;
            dr["Price"] = lblPrice;
            dr["quantity"] = quantitylist;
            dr["total"] = lbltotal;
            dr["image"] = image;

            dt.Rows.Add(dr);
            Session["addtocart"] = dt;
            cartQuantity= dt.Rows.Count.ToString();
            Master.OnlbCartCountMasterPage.Text = cartQuantity;
        }
        else
        {
            //lblErrorMessage.Text = "Item Already Added!";
        }

    }
    else
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Item", typeof(string));
        dt.Columns.Add("Price", typeof(int));
        dt.Columns.Add("quantity", typeof(int));
        dt.Columns.Add("total", typeof(int));
        dt.Columns.Add("image", typeof(string));
        DataRow dr = dt.NewRow();
        dr["ID"] = id;
        dr["Item"] = lblname;
        dr["Price"] = lblPrice;
        dr["quantity"] = quantitylist;
        dr["total"] = lbltotal;
        dr["image"] = image;
        dt.Rows.Add(dr);
        Session["addtocart"] = dt;
        cartQuantity=Convert.ToString(dt.Rows.Count);
    }
    Master.OnlbCartCountMasterPage.Text = cartQuantity;
}

按钮代码

protected void btnAddtoCart_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    ListViewItem item = (ListViewItem)btn.NamingContainer;
    HiddenField hfid = item.FindControl("hfId") as HiddenField;
    Label lblitem= item.FindControl("item") as Label;
    Label lblPrice = item.FindControl("lblPrice") as Label;
    DropDownList lblQuantity = item.FindControl("qtydrpdwn") as DropDownList;
    Label lblTotal = item.FindControl("Total") as Label;
    HiddenField hfimg = item.FindControl("imgpath") as HiddenField;

    add2cart(Convert.ToInt32(hfid.Value),lblitem.Text, Convert.ToInt32(lblPrice.Text), Convert.ToInt32(lblTotal.Text), Convert.ToInt32(lblQuantity.Text),hfimg.Value);
    btn.Enabled = false;

    //Response.Redirect("index.aspx");
}

我认为在页面加载中出现问题?因为会话无法识别来自add2cart方法的值。它显示添加到购物车的会话中的值,但不显示在页面加载中。我无法在代码中发现问题?

当我点击按钮添加到购物车。价值保持不变0根本没有增加?

1 个答案:

答案 0 :(得分:0)

试试这个。我将更新例程外包给一个单独的方法,每次按下按钮都会调用该方法。

String cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
string cartQuantity;

// this will be executed when the page is loaded at the beginning or when refreshed
protected void Page_Load(object sender, EventArgs e)
{
    // I don't know what this does, if you also need it for the update
    // then put it into the updateQuantity method
    if (!IsPostBack)
    {
        LoadList();
    }

    // update quantity
    updateQuantity();
}

// method to update the quantity
private void updateQuantity()
{
    if (Session["addtocart"] != null)
    {
        DataTable dt = new DataTable();
        cartQuantity = Convert.ToString(dt.Rows.Count);
    }
    else
    {
        cartQuantity = "0";
    }
    Master.OnlbCartCountMasterPage.Text = cartQuantity;
}


protected void btnAddtoCart_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    ListViewItem item = (ListViewItem)btn.NamingContainer;
    HiddenField hfid = item.FindControl("hfId") as HiddenField;
    Label lblitem= item.FindControl("item") as Label;
    Label lblPrice = item.FindControl("lblPrice") as Label;
    DropDownList lblQuantity = item.FindControl("qtydrpdwn") as DropDownList;
    Label lblTotal = item.FindControl("Total") as Label;
    HiddenField hfimg = item.FindControl("imgpath") as HiddenField;

    add2cart(Convert.ToInt32(hfid.Value),lblitem.Text, Convert.ToInt32(lblPrice.Text), Convert.ToInt32(lblTotal.Text), Convert.ToInt32(lblQuantity.Text),hfimg.Value);
    btn.Enabled = false;

    //call update method
    updateQuantity();
}