Search code examples
c#asp.netwebformsrepeater

How to obtain a textbox control from a div of repeater row?


I'm trying making a shoppingcart and when I press the updatebutton for 1 of the products to update the amount of that product I need to get the textbox that belongs to it.

To manage this I bounded the productID to a div where the textbox is in so I can get the right textbox. My question is how do I get the value of this textbox?

here is the div

<td id="Td1" class="cart_update" runat="server" style="border-width: 0px 1px 1px 0px;">
    <div id='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' runat="server">
        <asp:TextBox ID="tbx" Text='<%# DataBinder.Eval(Container.DataItem, "ProductAmount") %>' CssClass="carttextbox" runat="server" />
        <asp:Button ID="btn_update" runat="server" OnCommand="btn_update_Click" Text="Update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' />
        <asp:Button ID="btn" runat="server" OnCommand="btn_Click" Text="Remove" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' />
    </div>
</td>

this is what I have in my code behind so far

protected void btn_update_Click(object sender, CommandEventArgs e)
{
    int command = Convert.ToInt32(e.CommandArgument);
    foreach(RepeaterItem item in Repeater1.Items)
    {
        TextBox tbx = find the right textbox;
        if(tbx != null)
        {
            foreach (ShoppingCart r in cart.shoppingcart)
            {
               if (r.ProductID == command)
               {
                   r.ProductAmount = Convert.ToInt32(tbx.Text);
               }
            }
         }
    }
}

Solution

  • foreach(RepeaterItem item in Repeater1.Items)
    {
        var tbx = item.FindControl("tbx") as TextBox;
        if(tbx != null)
        {
            foreach (ShoppingCart r in cart.shoppingcart)
            {
               if (r.ProductID == command)
               {
                   r.ProductAmount = Convert.ToInt32(tbx.Text);
               }
            }
         }
    }