gpt4 book ai didi

c# - 从 html 按钮调用 C# 函数

转载 作者:行者123 更新时间:2023-12-05 07:08:20 24 4
gpt4 key购买 nike

解释

我在一个电子商务网站上工作,目前在购物车部分工作。现在所有添加到购物车的产品都存储在静态列表中。对于列表中的所有产品,都会生成 html 代码。

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>

每个都有一个用于从购物车中删除产品的按钮。

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

当我点击按钮从购物车中删除产品时,我想调用一个 C# 函数。

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

问题是什么

函数没有被调用。

我尝试了什么

  • 使用 html 元素输入而不是按钮
  • 调用一个调用 C# 函数的 jquery 函数
  • 使用表格

结论

感谢您查看我的问题!如果您知道任何解决方案或有其他选择,请告诉我。如果我没有 100% 正确地使用 Stack Overflow,请原谅,欢迎任何提示!

提前致谢!

最佳答案

解决方案在这里!!!

替换和工作!

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


public void btnDeleteProduct()
{
//Do stuff
}

关于c# - 从 html 按钮调用 C# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61901157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com