Call C# function from html button

Viewed 213

Explanation

I am working on an e-commerce website and I'm currently working on the cart section. Now all the products that are added to the cart are stored in a static list. For all the products in the list there will be html code generated.

protected void btnCart_Click(object sender, EventArgs e)
    {
        //Show howmany items are in the cart list
        lblCartCount.Text = "There are currently " + (CartList.Count()).ToString() + " items in your cart";

        //Clear current cart-html code
        CartProducts.InnerHtml = String.Empty;

        //Foreach product in the cart list
        for (int i = 0; i < CartList.Count; i++)
        {
            //Get necessary info about the current looped product
            var TupleProductInfo = B.GetProductInfo(CartList[i]);
            int ProductID = TupleProductInfo.Item1;
            String ProductSize = TupleProductInfo.Item2;
            double ProductPrice = TupleProductInfo.Item3;

                //Foreach product cleate cart-html
                CartProducts.InnerHtml += 
                    "<div class='CartProduct'>" +
                        "<div>" +
                            "<h4>" + B.GetProductName(ProductID) + "</h4>" +
                        "<button id='btnDeleteProduct' runat='server' onserverclick='btnDeleteProduct_click' title='remove product'>" +
                            "<i class='fas fa-times fa-lg'></i>" +
                        "</button >" +
                        "</div>" +
                        "<div>" +
                            "<p>Size: " + ProductSize + "</p>" +
                            "<p>Price: €" + Math.Round(ProductPrice, 2) + "</p>" +
                            "<p>Amount: " + CartListAmount[i].ToString() + "</p>" +
                        "</div>" +
                    "</div>";
            }

        //Show the cart 
        popupCart.Style.Add("display", "flex");
        }
        <div runat="server" id="CartProducts" class="margin2"></div>

Each with a button to delete the product from the cart.

<button id='btnDeleteProduct' runat='server' onserverclick='btnDeleteProduct_click' title='remove product'>

When I click the button to delete the product from the cart I want to call a C# function.

protected void btnDeleteProduct_click(object sender, EventArgs e){ ... }

What is the problem

The function is not getting called.

What I tried

  • use the html-element input instead of button
  • call a jquery function that call's a C# function
  • use a form

Conclusion

I appreciate that you took a look at my problem! If you know any solution or you have an alternative please let me know. Pleases excuse me if I didn't use Stack Overflow 100% correct, any tips are welcome!

Thanks in advance!

2 Answers

Solution Here !!!

Replace and Work !

"<button id='btnDeleteProduct' runat='server' OnClick=\"btnDeleteProduct()\"  title='remove product'>" +


public void btnDeleteProduct()
    {
        //Do stuff
    }

After trying for some time, I still coudn't get the function to be called (I tried the above suggestions). So I solved it as following: I made a Session with all the products of my cart and redirected to a separate page. On this page I made a datalist and wrote a SelectCommand to select all the items in the session

Related