ASP.net C#的GridView ButtonField字段onclick事件 [英] ASP.net C# Gridview ButtonField onclick event

查看:2001
本文介绍了ASP.net C#的GridView ButtonField字段onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个ASP code的网上商店类型的项目。存在着增加了产品ID为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.

我现在需要的是一种方法,一个ButtonField字段添加到GridView控件在那里我可以添加一个按钮++(如添加到购物车按钮产品页面上),正常的ASP按钮取的onclick =方法名但这些在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();}

这是code我用所有的按钮++。它采用button.id(buttonID =的productID)。所以,我需要一种方法让所有的按钮是相同的图像,但buttonID已被数据绑定到产品ID,因此它可以以某种方式执行该方法(带有某种中的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="").

我已经看了,并用Google搜索在过去的几个小时,但我不能似乎发现了什么。在过去,我发现计算器,所以我希望有人在这里可以帮助很多答案。

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#code背后创建你的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事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆