ASP.net button onclick not updating after the first click unless Response.Redirect or refresh

Viewed 44

I have a Add to Cart button that show different text and enabled/disabled based on the stock left and if it is within the cart of the user.

asp:Button CssClass="btn btn-outline-dark mt-auto" ID="Button1" runat="server" CommandArgument='<%# Eval("prod_id") %>' Text='<%# Convert.ToInt32(Eval("prod_quantity")) > 0 ? Convert.ToInt32(Eval("is_in_cart")) < 1 ? "Add To Cart" : "Already in Cart" : "Sold out" %>' Enabled='<%# Convert.ToInt32(Eval("prod_quantity")) > 0 ? Convert.ToInt32(Eval("is_in_cart")) < 1 ? true : false : false %>' OnClick="Button1_Click" />   

And this is my code behind to perform all the relevant SQL operations to update the database.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                String id = Session["id"].ToString();
                string test = Convert.ToString(Page.FindControl("HiddenField1"));
                string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strCon))
                {
                    string strHome = "SELECT p.prod_id, p.prod_name, p.prod_desc, p.prod_quantity, p.prod_price, p.prod_photo, COUNT(c.prod_id) AS is_in_cart FROM   product p LEFT OUTER JOIN cart_item c ON p.prod_id = c.prod_id WHERE  c.id = '" + id + "' OR c.id IS NULL GROUP BY p.prod_id, p.prod_name, p.prod_desc, p.prod_price, p.prod_photo, p.prod_quantity;";


                    using (SqlCommand cmdHome = new SqlCommand(strHome, con))
                    {
                        con.Open();
                        DataTable rstData = new DataTable();
                        rstData.Load(cmdHome.ExecuteReader());

                        RepeaterHome.DataSource = rstData;
                        RepeaterHome.DataBind();
                    }
                }
                
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            int prodId = Convert.ToInt32(btn.CommandArgument);

            String id = Session["id"].ToString();

            string strCon = ConfigurationManager.ConnectionStrings["WebConfigConString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strCon))
            {
                string strCart = "INSERT INTO CART_ITEM (id, prod_id, quantity) values (@id, @prod_id, @quantity)";

                using (SqlCommand cmdCart = new SqlCommand(strCart, con))
                {
                    try
                    {
                        con.Open();

                        cmdCart.Parameters.AddWithValue("@id", id);
                        cmdCart.Parameters.AddWithValue("@prod_id", prodId);
                        cmdCart.Parameters.AddWithValue("@quantity", 1);

                        SqlDataReader dtrCart = cmdCart.ExecuteReader();
                        dtrCart.Close();
                        con.Close();
          
                        } catch (SqlException)
                    {
                        Response.Write("<script>alert('Item already in cart!')</script>");
                    }
                }
               
            }
            Response.Redirect("home.aspx");
        }

However, when I first click the button, the database is updated but the web page will not be updated, and by second click it shows the changes of the first click therefore it is delayed. A refresh or a "Response.Redirect("xxx.aspx") as I've attempted above is the only way I've found to solve my problem right now, any idea?

0 Answers
Related