问题描述
我为一个网上商店类型的项目编写了一个 ASP 代码.有一种方法可以将 ProductID 添加到 cookie 并在产品数量上加 1.另一种方法读取 cookie,将 cookie 转换为数据表并将其放入 gridview.这个gridview 基本上就是shoppingcart.aspx 页面.
I've written an ASP code for a webshop type project. There is a method that adds the ProductID to a cookie and adds 1 to the quantity of the product. Another method reads the cookie, transforms the cookie to a data table and puts it in a gridview. This gridview basically is the shoppingcart.aspx page.
我现在需要的是一种将按钮字段添加到该网格视图的方法,我可以在其中添加 ++ 按钮(如产品页面上的添加到购物车按钮),普通的 asp 按钮采用 onclick="methodname"
但gridview中的这些没有.
What i now need is a way to add a buttonfield to that gridview where i can add a ++ button (like the add to shopping cart button on a product page), normal asp buttons take a onclick="methodname"
but these in the gridview don't.
add1ToShoppingCart(string productId)
{[...]shoppingCartCookie[productId] = (amount + 1).ToString();}
这是我用于所有 ++ 按钮的代码.它需要 button.id (buttonID=productID).所以我需要一种方法让所有按钮都是相同的图像,但是 buttonID 必须数据绑定到 productID 以便它可以以某种方式执行该方法(使用某种 onclick=""
).
That is the code I use for all the ++ buttons. It takes the button.id (buttonID=productID). So I need a way to have all the buttons be the same image, but the buttonID has to be databound to the productID so it can somehow execute that method (with some sort of onclick=""
).
在过去的几个小时里,我一直在寻找和搜索,但我似乎找不到任何东西.过去我在stackoverflow上找到了很多答案,所以我希望这里有人可以提供帮助.
I've looked and googled for the past couple hours but I cant seem to find anything. In the past I found a lot of answers on stackoverflow so I hoped somebody here could help.
提前致谢.
推荐答案
你可以使用模板字段:
<asp:TemplateField ItemStyle-Width="33px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
ShowHeader="false" HeaderStyle-Height="40px">
<ItemTemplate>
<asp:ImageButton ID="btnAddToCard" runat="server" ImageUrl="~/Images/btnAddToCard.png" CausesValidation="false"
CommandName="AddToCard" CommandArgument='<%# Eval("ID") %>'
/>
</ItemTemplate>
<HeaderStyle Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="33px"></ItemStyle>
</asp:TemplateField>
并在您背后的 C# 代码中创建 gridview 的以下事件并执行您想要的操作:
and in your C# code behind you create the following event of your gridview and do what you want :
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCard")
{
//Write code to add to card
}
}
GV 是我的 GridView 的 ID!
GV is the ID of my GridView !
这篇关于ASP.net C# Gridview ButtonField onclick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!