문제

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);
               }
            }
         }
    }
}
도움이 되었습니까?

해결책

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

다른 팁

I would use the .FindControl() method. this: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeateritem.findcontrol%28v=vs.110%29.aspx

Or in more specific cases, I already found useful to simply loop (recursively) across item.Controls.

Use following code

foreach (RepeaterItem lvi in myRepeater.Items) {


TextBox tb = (TextBoxcb )lvi.FindControl("myTextBox");


}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top