使用行索引从GridViewRow获取信息

时间:2013-12-01 22:18:57

标签: c# asp.net gridview

我正在尝试建立一个购物车,但我不知道将物品放入购物车。

我正在使用列表加载我的项目列表:

<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];
          }
       }

问题是,在我得到&#39;行后#39;对象,它只有网格索引,我不知道如何使用行中包含的信息将其强制转换回对象。

我很感激帮助:)

2 个答案:

答案 0 :(得分:1)

处理此问题的更简单方法是,您应该将数据绑定到Tex中控件的ItemTemplate属性。

所以你的GridView标签应该是这样的:

<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='<%# Bind("Nombre")%>'></asp:Label>
                            <br />
                            <asp:Label ID="lblCategory" runat="server" Text='<%# Bind("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="AddToCart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to cart" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:TemplateField>

请注意 btnAddToCart 按钮,其CommandName应设置为“AddToCart”,而不是代码中的“添加到购物车”。

然后在你的RowCommand事件处理程序中,你可以使用代码 penjepitkertasku 回答

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];

        //get key setting in DataKeyNames
        string id = gridItems.DataKeys[index].Value.ToString();

        //get value from Controls in ItemTemplate
        string name = ((Label)(row.FindControl("lblName"))).Text;
        string category = ((Label)(row.FindControl("lblCategory"))).Text;
        string cantidad = ((TextBox)(row.FindControl("txtCant"))).Text;

    }
}

答案 1 :(得分:0)

您可以使用:

//get row
GridViewRow gvr = ((GridViewRow)(((Button)e.CommandSource).NamingContainer));

//get datakey
string id = gridItems.DataKeys[Convert.ToInt32(gvr.RowIndex)].Value.ToString();

//get field value
string name = ((Label)(gvr.FindControl("lblName"))).Text;
string category = ((Label)(gvr.FindControl("lblCategory"))).Text;
string cantidad = ((TextBox)(gvr.FindControl("txtCant"))).Text;

获取行中包含的信息