title: How to access specific controls in a ASP.Net Repeater
body: <p><strong><em>Introduction</em></strong></p>
  
  <p>I am working on a college project where I need to build a shopping website.</p>
  
  <p>I am currently building a "Manage Shopping Cart" page with the following structure:
  <a href="https://i.stack.imgur.com/xEnLf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xEnLf.png" alt="enter image description here"></a></p>
  
  <p>What is important for my question is the "products". The "products" is a ASP.Net Repeater that only uses the <code>ItemTemplate</code> control. The repeater is then populated with a list of products in a user's cart.</p>
  
  <p><strong><em>What is my problem?</em></strong></p>
  
  <p>The particular area I am having trouble with is the "Save" Button.
  The user can change the quantity of each product they want; however, they must click the "Save" button for the information to go to the database.</p>
  
  <p>The trouble is accessing the <strong>correct</strong> quantity textbox inside my Save button's onclick event.</p>
  
  <p>How can I access the correct text box in all of my Save button's OnClick Events?</p>
  
  <p><strong><em>What have I tried?</em></strong></p>
  
  <p>I was initially thinking of using the WebControl.FindControl method.
  Of course though, the FindControl requires an ID and I would need different IDs for each textbox.</p>
  
  <p>In order to the fix this ID problem, I tried...</p>
  
  <p><strong><em>firstly...</em></strong></p>
  
  <p>Tried binding the productId to the textbox id.</p>
  
  <pre><code>&lt;asp:TextBox ID='quantity&lt;%#Eval("productId") %&gt;' runat="server" TextMode="Number" min="1" Text='&lt;%#Eval("selectedQuantity") %&gt;' max='&lt;%#Eval("availableQuantity") %&gt;' /&gt;
  </code></pre>
  
  <p>I then found out that is not is possible; because, when setting an ID this way, you must use "simple ids" (Example: "button1"). Supposedly I could set the ID in the code-behind though.</p>
  
  <p><strong><em>Secondly...</em></strong></p>
  
  <p>I then tried using the Repeater's OnItemCreated Event to dynamically set the ID of the TextBox to "quantityX", where X is an Product ID.</p>
  
  <p>What I did in is bind the productId into a WebControl attribute:</p>
  
  <pre><code>&lt;asp:TextBox ID='quantity' productId='&lt;%#Eval("productId") %&gt;' runat="server" TextMode="Number" min="1" Text='&lt;%#Eval("selectedQuantity") %&gt;' max='&lt;%#Eval("availableQuantity") %&gt;' /&gt;
  </code></pre>
  
  <p>After this, I then using the <code>FindControl()</code> method to get TextBoxs with an ID of "quantity". After finding the TextBox, I would take the productId attribute and append its value to the TextBox ID.</p>
  
  <pre><code>protected void productsList_ItemCreated(object sender, RepeaterItemEventArgs e)
  {
      if(e.Item.ItemType == ListItemType.Item)
      {
          TextBox quantity = (TextBox)e.Item.FindControl("quantity");
          quantity.ID = String.Format("quantity{0}", quantity.Attributes["productId"]);
      }
  }
  </code></pre>
  
  <p>I then found out that the productId attribute did not have a value; because, the DataBinding has not occurred. In otherwords, <code>Eval("productId")</code> was not running.</p>
  
  <p><strong><em>thirdly...</em></strong></p>
  
  <p>I took the same approach as the one before with 1 difference. I used the Repeater's <code>OnItemDataBound</code> event instead of the <code>OnItemCreated</code> Event.</p>
  
  <pre><code>protected void productsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
      if(e.Item.ItemType == ListItemType.Item)
      {
          TextBox quantity = (TextBox)e.Item.FindControl("quantity");
          quantity.ID = String.Format("quantity{0}", quantity.Attributes["productId"]);
      }
  }
  </code></pre>
  
  <p>This partially worked; however, there was one problem. The product with the biggest Product ID was skipped. (E.X, if I had products 4, 32, and 68. The Product ID of 68 would be skipped)</p>
  
  <p>As you can see in the image below, we have 3 products. I edited some text the picture to clearly point out the TextBoxs (as they are rendered).</p>
  
  <p>The IDs present are 1, 32 and 34. If you look at the ID attribute of 1 and 32, you will see they have "quantity" with their Product ID appended. </p>
  
  <p>Now look at the ID attribute of 34. The ID attribute is only "quantity".</p>
  
  <p>Now I am kinda of stuck. </p>
  
  <p><a href="https://i.stack.imgur.com/uTFXM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uTFXM.png" alt="enter image description here"></a></p>
  
  <p><strong><em>What am I using?</em></strong></p>
  
  <ul>
  <li>Visual Studio 2017</li>
  <li>.NET Framework 4.6.1</li>
  <li><strong><em>2-Tier</em></strong> WebForms Project</li>
  <li>Google Chrome</li>
  </ul>