問題描述
我為一個網上商店類型的項目編寫了一個 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 事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!