Note that there are some explanatory texts on larger screens.

plurals
  1. POFiring different linkbuttons in a Repeater and saving the value of each in an arraylist
    text
    copied!<p>Im using a repeater to display some products in an online shop for a school project. This is how the front end looks with the repeater</p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server" OnItemCommand="rptList_ItemCommand"&gt; &lt;ItemTemplate&gt; &lt;span style="float:left; padding:25px;" class="backgrnd"&gt; &lt;asp:ImageButton ID="imgProd" runat="server" style="width:150px; height:150px;" ImageUrl='&lt;%# DataBinder.Eval(Container.DataItem, "productImg")%&gt;' CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "productID")%&gt;' CommandName="ViewIndividProd"/&gt;&lt;br /&gt; &lt;p style="clear:left;"&gt; &lt;asp:Label ID="lbName" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "productName")%&gt;' /&gt;&lt;br /&gt; &lt;asp:Label ID="lbUnitPrice" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "unitPrice")%&gt;'/&gt;&lt;br /&gt; &lt;asp:Label ID="lbRatings" runat="server" Text=''&gt;Ratings&lt;/asp:Label&gt;&lt;br /&gt; &lt;asp:LinkButton ID="linkCart" runat="server" CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "productID")%&gt;' CommandName="AddToCart"&gt;Add to Cart&lt;/asp:LinkButton&gt; &lt;/p&gt; &lt;/span&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>As you can see I've added on the OnItemCommand in the Repeater tag so that this is invoked whenever one of the buttons(image/link) is fired. That works perfectly fine for both commandname <code>AddToCart</code> and <code>ViewIndividProd</code>. However, i want to store the the productid of a specific item that was invoked by the particular button. In my case now, it only stores ONE productid in the arraylist at a time and 'forgets' the productid that was stored previously when another linkbutton is clicked. <br><br><b>Question</b> How do i make it such that everytime a linkbutton in the repeater is fired, it remembers the productid pertaining to the linkbutton that was fired and save these ids into the arraylist? This is how the back end looks</p> <pre><code>ArrayList cart = new ArrayList(); protected void rptList_ItemCommand(object sender, RepeaterCommandEventArgs e) { if (e.CommandName == "ViewIndividProd") { Session["productID"] = e.CommandArgument.ToString(); Response.Redirect("IndividProduct.aspx"); } if (e.CommandName == "AddToCart") { string prodid = e.CommandArgument.ToString(); cart.Add(prodid); Session["ShoppingCart"] = cart; Response.Redirect("IndividCat.aspx"); } msg.Text = "Shopping cart: " + String.Join(",", cart.ToArray()); } </code></pre> <p>Your feedback would be much appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload