C# 将值从GridView传递到ASP.NET中的代码隐藏文件
我有一个GridView,它绑定数据库中的值。 代码如下:C# 将值从GridView传递到ASP.NET中的代码隐藏文件,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,它绑定数据库中的值。 代码如下: <asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="Produc
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Button" CommandName="AddToCart" />
</Columns>
</asp:GridView>
我想将包含txtQuantity和ProductID值的命令参数传递给CommandName“AddToCart”上的代码。
如何执行此操作?在GridView中绑定ProductID和Quantity的值。在GridView中,如下所示:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity" Text="<%# Bind("Quantity")%>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Button Text="Button" CommandName="AddToCart" CommandArgument="<%# Eval("Quantity") + "," + Eval("ProductID")%>" />
</Columns>
</asp:GridView>
我试过了,但是我得到的错误是文字内容('检查你是否在GridView中的每个属性之间提供了空间?Van,我在项目模板中添加了按钮。我想这就是他得到文字内容的原因。'我认为asp:ButtonField没有CommandArgument属性,而asp:button有CommandArgument属性。
if(e.CommandName == "AddToCart")
{
string[] args = e.CommandArgument.ToString().Split(",");
Decimal Quantity = Convert.ToDecimal(args[0]);
int ProductID = Convert.ToInt32(args[1]);
}