Accessing a "buried control" in a repeater from code-behind in C#

mynameiskristopher used Ask the Experts™
I am trying to access a "buried control" in a repeater from the code behind page in C# .NET

All I need to know is how to access the value of the textbox... however I believe the findcontrol method may make a copy of the object instead of a reference to the object. However I am new to OOP concepts and am a little lost.

I do know this works flawlessly if I put the "quantity" textbox outside of the repeater and don't use the findcontrol method.

I have a show products page:

<asp:Content ID="main_text" ContentPlaceHolderID="main_text" Runat="Server">
    <asp:Label ID="lblErrorMsg" runat="server" Text="" ForeColor="Red"></asp:Label>
<form runat="server" method="post" action="show_product.aspx">
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:online_store_display_ConnectionString %>"
        SelectCommand="SELECT id, name, image, price, display, description FROM products WHERE (id = @id) AND (display = 1)">
            <asp:QueryStringParameter DefaultValue="" Name="id" QueryStringField="id" />
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2" EnableViewState="false">
                  <img src='<%# Eval("image") %>' hspace="4" vspace="2" alt="" align="left"/>
                <br />
                <% %>
                <b>Price:</b> $<%#Eval("price")%>
            <br style="clear:both;" />
                Quantity:     <asp:textbox ID="quantity" runat="server" EnableViewState="false" />
                <asp:Label ID="product_id" runat="server" Text='<%#Eval("id")%>' Visible="false"></asp:Label>
                <br clear="all" />
                  <div align="center"><asp:button ID="add_to_cart" text="Add to cart" runat=server OnClick="add_to_cart" /></div>

The code behind is as follows:
public partial class show_product : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (Session["cart"] == null)
            shoppingcart cart1 = new shoppingcart();
            Session["cart"] = cart1;
    protected void add_to_cart(object sender, EventArgs e)
        int quantitytmp;
        int prod_idtmp;
        shoppingcart cart = (shoppingcart)Session["cart"];
        TextBox quantityTxt = (TextBox)Repeater1.FindControl("quantity");

            quantitytmp = Int32.Parse(quantityTxt.Text);
            prod_idtmp = Int32.Parse(Request.QueryString["id"]);
            cart.Add_Item(prod_idtmp, quantitytmp);
            lblErrorMsg.Text = "Product Added To Shopping Cart";
        //    lblErrorMsg.Text = "Quantity Must Be Numeric";
        //set the session back to the object
        Session["cart"] = cart;


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Obviously you need to loop through the items of the repeater before you can FindControl the correct TextBox

foreach(RepeaterItem ri in Repeater1){
  TextBox tb = (TextBox)ri.FindControl('textboxid");
    // do your thing with tb
Small mistake
oreach(RepeaterItem ri in Repeater1.Items){
Here's a method I use all the time for finding controls in a GridView. It should work for you:

public Control FindControl(string ControlId, ControlCollection collection)
            foreach (Control control in collection)
                if (control.ID == ControlId)
                    return control;

                if (control.HasControls())
                    Control nestedControl = FindControl(ControlId, control.Controls);

                    if (nestedControl != null)
                        return nestedControl;
            return null;


FindControl("quantity", Repeater1.Controls) will return your textbox.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial