Sometimes we need to add the data in a page from code behind in asp.net webform project & that types of situation arise when we need to add a list of data using a loop or something. In that types of situation, we need to use the repeater from the code behind for good coding. Now, first of all, we need to know what is repeater? And it is given in the bellow.
What is Repeater Control
The Repeater Web server control is a data-bound container control that produces a list of individual items. You define the layout of individual items on a Web page by using templates. When the page runs, the control repeats the layout for each item in the data source.
What will we do next?
We will create an aspx page for showing a bunch of product which will same as a basket page. And also we need to add some in code behind for getting the product data which is stored in the session. After getting the data we will bind the data in the repeater. So now let’s go to the main business.
Basket.aspx
<div class="main-content"> <div class="grid"> <div class="col-1-2 basket-img"><h4>Product Image</h4></div> <div class="col-1-2 basket-detail"><h4>Product Details</h4></div> <div class="col-1-2 basket-action"><h4>Remove Product</h4></div> </div><hr /> <asp:Repeater ID="BasketProductRepeater" runat="server" ItemType="YourProjectName.Basket.BasketProductList"> <ItemTemplate> <div class="grid"> <div class="col-1-2 basket-img"> <div class="image"> <img style="width: 135px !important;" src="<%# Item.productImg %>"> </div> </div> <div class="col-1-2 basket-detail"> <h4><%# Item.productTitle %></h4> <p><%# Item.productDescription %></p> <p class="price"><%# Item.productCost %></p> </div> <div class="col-1-2 basket-action"> <asp:Button ID="RemoveProductButton" runat="server" Text="X" OnCommand="RemoveProductButton_Click" CommandArgument="<%# Item.productId %>"/> </div> </div> <hr /> </ItemTemplate> </asp:Repeater> <p style="color:red"><asp:Literal runat="server" id="ProductIDLiteral" /></p> </div>
Basket.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace YourProjectName { public partial class Basket : System.Web.UI.Page { public List<BasketProductList> basketProduct = new List<BasketProductList>(); protected void Page_Load(object sender, EventArgs e) { //Get All Product Data From Session var product = Session["allProduct"] != null ? (Dictionary<int, List<string>>)Session["allProduct"] : null; //Add All Product In The basketProduct If Any Product Found if (product.Count > 0) { foreach (var allProduct in product) { BasketProductList r = new BasketProductList() { productId = allProduct.Key, productTitle = allProduct.Value[0], productDescription = allProduct.Value[1], productCost = allProduct.Value[2], productImg = allProduct.Value[3] }; basketProduct.Add(r); } //Add basketProduct With The Repeater To Show In The Frontend if (!Page.IsPostBack) { BasketProductRepeater.DataSource = basketProduct; BasketProductRepeater.DataBind(); } } } // End of Page_Load() } // End of Basket class } // End of namespace