title: How to get the hidden field value for a specific repetition within a repeator in ASP .net?
body: <p>Here is my ASPX markup to display all the products available from the database, and allow the user to view details and add to cart:</p>
  <pre><code>&lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/Site1.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;home.aspx.cs&quot; Inherits=&quot;WADAssignment.WebForm1&quot; %&gt;
  &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;head&quot; runat=&quot;server&quot;&gt;
  
  &lt;/asp:Content&gt;
  &lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;ContentPlaceHolder1&quot; runat=&quot;server&quot;&gt;
      &lt;form runat=&quot;server&quot;  &gt;
  &lt;section class=&quot;py-5&quot;&gt;     
              &lt;div class=&quot;container px-4 px-lg-5 mt-5&quot;&gt;
                  &lt;div class=&quot;row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center&quot;&gt;
                      &lt;asp:Repeater ID=&quot;RepeaterHome&quot; runat=&quot;server&quot;&gt;
                      &lt;ItemTemplate&gt;    
                          &lt;div class=&quot;col mb-5&quot;&gt;
                          &lt;div class=&quot;card h-100&quot;&gt;
                              &lt;!-- Product image--&gt;
                              &lt;img class=&quot;card-img-top&quot; width=&quot;205.99px&quot; height=&quot;205.99px&quot; src=&quot;data:image/jpg;base64,&lt;%# @Convert.ToBase64String((byte[])(Eval(&quot;prod_photo&quot;))) %&gt;&quot; alt=&quot;...&quot; /&gt;
                              &lt;!-- Product details--&gt;
                              &lt;div class=&quot;card-body p-4&quot;&gt;
                                  &lt;div class=&quot;text-center&quot;&gt;
                                      &lt;!-- Product name--&gt;
                                      &lt;asp:HiddenField ID=&quot;prodId&quot; Value='&lt;%# Eval(&quot;prod_id&quot;) %&gt;' runat=&quot;server&quot; /&gt;
                                      &lt;h5 class=&quot;fw-bolder&quot;&gt;&lt;%# Eval(&quot;prod_name&quot;) %&gt;&lt;/h5&gt;
                                      &lt;!-- Product price--&gt;
                                      RM &lt;%# Eval(&quot;prod_price&quot;) %&gt;&lt;br/&gt;
                                      &lt;%# Eval(&quot;prod_desc&quot;) %&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              
                              &lt;!-- Product actions--&gt;
                              &lt;div class=&quot;card-footer p-4 pt-0 border-top-0 bg-transparent&quot;&gt;
                              &lt;div class=&quot;text-center&quot;&gt;
                                  &lt;asp:Button CssClass=&quot;btn btn-outline-dark mt-auto&quot; ID=&quot;Button2&quot; runat=&quot;server&quot; Text=&quot;View Details&quot; PostBackUrl='&lt;%# &quot;productDetails.aspx?id=&quot; + Eval(&quot;prod_id&quot;) %&gt;'/&gt;
  
  &lt;/div&gt;&lt;br /&gt;
  
  
  &lt;div class=&quot;text-center&quot;&gt;
                                  &lt;asp:Button CssClass=&quot;btn btn-outline-dark mt-auto&quot; ID=&quot;Button1&quot; runat=&quot;server&quot; Text='&lt;%# Convert.ToInt32(Eval(&quot;prod_quantity&quot;)) &gt; 0 ? &quot;Add to Cart&quot; : &quot;Sold out&quot; %&gt;' Enabled='&lt;%# Convert.ToInt32(Eval(&quot;prod_quantity&quot;)) &gt; 0 ? true : false %&gt;' OnClick=&quot;Button1_Click&quot; /&gt;   
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:Repeater&gt;
                      
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/section&gt;
          &lt;/form&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  <p>This is the <code>onclick</code> function for the add to cart button, however with my current implementation of going through the repeater item list, the biggest prodId will always be selected, instead of the respective one, and then causing a SQL error.</p>
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
          HiddenField hidden = null;
  
          foreach (RepeaterItem item in RepeaterHome.Items)
          {
              hidden = (HiddenField)item.FindControl(&quot;prodId&quot;);
          }
  
          int prodId = Convert.ToInt32(hidden.Value);
          String id = Session[&quot;id&quot;].ToString();
  
          string strCon = ConfigurationManager.ConnectionStrings[&quot;WebConfigConString&quot;].ConnectionString;
          using (SqlConnection con = new SqlConnection(strCon))
          {
              string strCart = &quot;INSERT INTO CART_ITEM (id, prod_id, quantity) values (@id, @prod_id, @quantity)&quot;;
              string strUpdateProd = &quot;UPDATE PRODUCT SET PROD_QUANTITY = PROD_QUANTITY - 1 WHERE PROD_ID = @prod_id&quot;;
  
              using (SqlCommand cmdCart = new SqlCommand(strCart, con))
              {
                  try
                  {
                      con.Open();
  
                      cmdCart.Parameters.AddWithValue(&quot;@id&quot;, id);
                      cmdCart.Parameters.AddWithValue(&quot;@prod_id&quot;, prodId);
                      cmdCart.Parameters.AddWithValue(&quot;@quantity&quot;, 1);
  
                      SqlDataReader dtrCart = cmdCart.ExecuteReader();
                      dtrCart.Close();
                      con.Close();
  
                      using (SqlCommand cmdUpdateProd = new SqlCommand(strUpdateProd, con))
                      {
                          con.Open();
                          cmdUpdateProd.Parameters.AddWithValue(&quot;@prod_id&quot;, prodId);
  
                          SqlDataReader dtrUpdateProd = cmdUpdateProd.ExecuteReader();
                          dtrUpdateProd.Close();
                          con.Close();
                      }    
                      } catch (SqlException)
                  {
                      Response.Write(&quot;&lt;script&gt;alert('Item already in cart!')&lt;/script&gt;&quot;);
                  }
              }
             
          }
  }
  </code></pre>
  <p>This screenshot is showing my web page:</p>
  <p><a href="https://i.stack.imgur.com/DpC4v.png" rel="nofollow noreferrer">My product page</a></p>