在命令参数中查找转发器中的控件

时间:2010-09-04 21:34:49

标签: c# asp.net repeater

我有一个转发器:

        <asp:Repeater ID="rpt_Items" OnItemCommand="rpt_Items_ItemCommand" runat="server">
            <ItemTemplate>
                <div class="item">
                    <div class="fr">
                        <asp:TextBox ID="tb_amount" runat="server">1</asp:TextBox>
                        <p>
                            <%# Eval("itemPrice") %>
                        </p>
                        <asp:LinkButton ID="lb_buy" CommandName="buy" runat="server">buy</asp:LinkButton>
                    </div>
                    <asp:HiddenField ID="hdn_ID" Value='<%# Eval("itemID") %>' runat="server" />
                </div>
            </ItemTemplate>
        </asp:Repeater>

在转发器命令参数上,我想获取文本框和隐藏字段但是我该怎么做?

protected void rpt_Items_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "buy")
    {
        //ADD ITEM TO CART
        Response.Write("ADDED");


        Product getProduct = db.Products.FirstOrDefault(p => p.ProductID == id);
        if (getProduct != null)
        {
            CartProduct product = new CartProduct()
            {
                Name = getProduct.ProductName,
                Number = amount,
                CurrentPrice = getProduct.ProductPrice,
                TotalPrice = amount * getProduct.ProductPrice,
            };
            cart.AddToCart(product);
        }

    }
}

非常感谢!

1 个答案:

答案 0 :(得分:4)

您不必通过命令参数传递它,您可以在e.Item.FindControl()方法中使用rpt_Items_ItemCommand,如:

TextBox tb_amount = (TextBox)e.Item.FindControl("tb_amount");
HiddenField hdn_ID = (HiddenField)e.Item.FindControl("hdn_ID");