【问题标题】:Get information from GridViewRow using row index使用行索引从 GridViewRow 获取信息
【发布时间】: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];
          }
       }

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

感谢您的帮助:)

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

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

    所以你的 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;
    
        }
    }
    

    【讨论】:

    • 谢谢!实际上,我已经通过做同样的事情解决了这个问题,但是使用了一个文本框。从来没有意识到问题是我以前从未将数据绑定到文本字段。
    【解决方案2】:

    你可以使用:

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

    获取行中包含的信息

    【讨论】:

    • 谢谢,现在它确实带来了文本框,但标签字段返回为空:( 我一直在挖掘,似乎加载在客户端而不是服务器中的控件存在问题,尤其是标签。
    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多