简体   繁体   中英

Strange ASP.net label and postback issue

I'm going to be as accurate and descriptive as I can with this error as at the moment as far as I can see, it seems to be random.

To give some back ground info, I am making an online shop, the users click on an 'add' button to add item to their cart from a template field of my gridview. This takes the item they want to purchase and inserts it into a cart database table, this table is then shown through a gridview when the 'cart'icon is clicked to show the contents.

Now when the user clicks the item, checks are run to ensure the item isn't already in the basket. If it is in there it updates it to the new amount, and if it isn't it inserts it into the cart database whilst also updating the label to show the quantity of items. This all works fine apart from I have the frustrating error that sometimes the button simply wont add or update the cart or label total when the item is clicked. This seems to happen with the very first grid item most often.

I have stepped through the debugger and it seems to all be working, the correct ID is being obtained from the gridrow etc. I am wondering if it is some sort of postback issue

The code, load method to set the cart contents in the site master onload method:

     protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["usersName"] != null)
        {
            object a = Session["_id"];
            IDMaster = Convert.ToInt32(a);

            UserWelcome.Text = "Welcome back " + Session["usersName"].ToString();
            cartButton.Visible = true;
            Label1.Visible = true;
            Label2.Visible = true;
            memberLogin.Visible = false;
            LblOr.Visible = false;
            lblPlease.Visible = false;
            memberLogin.Visible = false;
            memberReg.Visible = false;

        }

        else
        {
            cartButton.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
            UserWelcome.Visible = false;
            memberLogin.Visible = true;
            memberLogout.Visible = false;
            LblOr.Visible = true;
            lblPlease.Visible = true;
            memberLogin.Visible = true;
            memberReg.Visible = true;

            MenuItem foo = NavigationMenu.FindItem("My Orders"); // your particular item
            NavigationMenu.Items.Remove(foo);

        }

        setCart();

    }

    private void setCart()
    {

        int cartItems;
        string cartNos;

        SqlConnection sqlcon = new SqlConnection(connString);
        SqlCommand sqlcmd = new SqlCommand("SELECT * FROM contents WHERE memberID = " + IDMaster + "", sqlcon);
        SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);


        if (ds.Tables[0].Rows.Count > 0)
        {
            cartItems = ds.Tables[0].Rows.Count;
            cartNos = cartItems.ToString();
            Label1.Text = cartNos;
        }


        else
        {

            int noItems = 0;
            string convertEmptyItems;

            convertEmptyItems = noItems.ToString();
            Label1.Text = convertEmptyItems;
        }

    }

Code used to add or update cart dependent on if the items already excists:

        GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
        TextBox t = (TextBox)row.FindControl("txtQuan");

        object ID = GridView1.DataKeys[row.RowIndex].Value;

        rowID = Convert.ToInt32(ID);

        ViewState["ID"] = rowID;

        string qty = t.Text;

        int stockToAdd = Convert.ToInt32(qty);

        DBHandler add = new DBHandler(rowID);

        int qtyCheck = add.getStockQty();

        if (stockToAdd > qtyCheck)
        {

            Button2_ModalPopupExtender.Show();

        }

        else{


            SqlConnection con;
            con = add.openDB();

            con.Open();

            DBHandler idCheck = new DBHandler(rowID);
            int rows = idCheck.checkCartRows();

            if (rows > 0)
            {
               int qtyNow = idCheck.getCartQty();

                int updateStock = qtyNow + stockToAdd;

                idCheck.updateQty(rowID, updateStock);
                updatePanel();

            }

            else
            {

                idCheck.insertCart(IDMaster, qty);
                updatePanel();

            }
            add.close();

            DBHandler updateCart = new DBHandler(IDMaster);

            int newRows = updateCart.checkCartRowsAfterUpdate();

            string numberOfRows = newRows.ToString();

            ((Label)this.Master.FindControl("Label1")).Text = numberOfRows;

        }


        }

Sorry for being vague about this question, but I really can pin it down! Hopefully someone will see something I am doing wrong.

Ok sorted this. It turned out to be something I had missed out. Basically when i displayed my cart contents i was displaying it for all members carts. This what was confusing me as at this point it was adding the item. My 'cart' will only show the contents for the particular member logged in through a session variable. As my cart checks to see if the item has been added it was seeing all of the items added for all members, so of course if another member had already added the item in the cart, it was never going to add it, it was simply going to update it and as it was adding it for the other member the label wasn't going to update. As soon as I deleted the cart contents i could add it. So basically I have now fixed my code so that each item has the corresponding ID of the member that added it and it works fine.

The key rule here is check all processes of your code for silly things that are missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM