【发布时间】:2013-12-01 22:18:56
【问题描述】:
我正在尝试构建一个购物车,但我无法将商品放入购物车。
我正在使用列表加载我的项目列表:
<asp:GridView ID="gridItems" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3" Height="227px"
onselectedindexchanged="GridView1_SelectedIndexChanged1" Width="651px"
HorizontalAlign="Center" DataKeyNames="ID" style="margin-top: 0px" OnRowCommand = "gridItems_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="center" style="width:37%; height: 146px;">
<tr>
<td class="style5" colspan="2">
<asp:Image ID="imgArt" runat="server" Height="110px" Width="160px"
ImageUrl=<%# DataBinder.Eval(Container.DataItem,"Imagen")%> />
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Label ID="lblName" runat="server" CssClass="textEntry"
style="text-align: center" Text=""><%# DataBinder.Eval(Container.DataItem,"Nombre")%></asp:Label>
<br />
<asp:Label ID="lblCategory" runat="server"><%# DataBinder.Eval(Container.DataItem,"Categoria")%></asp:Label>
</td>
</tr>
<tr>
<td class="style6">
Cantidad:
<asp:TextBox ID="txtCant" runat="server" CssClass="textEntry" Height="25px"
Width="33px">1</asp:TextBox>
</td>
<td class="style7">
<asp:Button ID="btnAddToCart" CssClass = "textEntry" runat="server" CommandName="Add to cart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to cart" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
点击该行后,我用 RowCommand 处理事件:
protected void gridItems_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = this.gridItems.Rows[index];
}
}
问题是我得到“行”对象后,它只有网格索引,我不知道如何使用行中包含的信息将其转换回对象。
感谢您的帮助:)
【问题讨论】: