Introduction
JavaScript function and Repeater are at the heart of our article. I have used these in web applications for cart calculation.
- Total Amount of Cart Item
- Less (-) Discount
- Add (+) Shipping Charges
- Amount To Pay
What is a Repeater?
Repeater has the following templates
- Item Template- It renders your render data from the DataSource.
- Alternating Item Template- It allows you to change render behavior for altering the item template from the DataSource.
- Header Template- For setting your header.
- Footer Template- For setting your footer.
- Separator Template- It allows you to design separation between the two items. Here, you can use <HR> or anything else which is good for your page.
To know more about Repeater, the links given below are the Microsoft MSDN links for more details and examples.
https://msdn.microsoft.com/en-us/library/x8f2zez5(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/x8f2zez5(v=vs.140).aspx
The naming convention of Repeater is rpt.
Example
https://msdn.microsoft.com/en-us/library/x8f2zez5(v=vs.140).aspx

- <asp:Repeater ID="rptCartItem" runat="server">
- <HeaderTemplate>
- </HeaderTemplate>
- <ItemTemplate>
- </ItemTemplate>
- <AlternatingItemTemplate>
- </AlternatingItemTemplate>
- <SeparatorTemplate>
- </SeparatorTemplate>
- <FooterTemplate>
- </FooterTemplate>
- </asp:Repeater>
Step by Step



If in your solution App_Code, the folder is not created, then by pressing YES, this will create App_Code as well as add DBML file inside the same folder.

Create a table called tblCarts. The table structure of our carts is given below.
- USE [MBKTest]
- GO
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[tblCarts](
- [ProductID] [int] IDENTITY(1,1) NOT NULL,
- [ProductName] [nvarchar](50) NOT NULL,
- [ProductImage] [nvarchar] (1000) NOT NULL,
- [ProductRate] [int] NULL
- ) ON [PRIMARY]
- GO



Inside Repeater, write the code given below.
- <asp:Repeater ID="rptCartItem" runat="server">
- <HeaderTemplate>
- </HeaderTemplate>
- <ItemTemplate>
- <div>
- <asp:Image ID="imgProduct" runat="server" ImageUrl='<%# Eval("ProductImage") %>' Width="5%" Height="7%" />
- <br />
- <b><asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label> </b>
- <p style="margin-left:500px"><b><u>Price: <asp:Label ID="lblProductRate" Class="pricecss" runat="server" Text='<%# Eval("ProductRate") %>'></asp:Label></b></u>
- </p>
- </div>
- </ItemTemplate>
- <SeparatorTemplate>
- <hr/>
- </SeparatorTemplate>
- <FooterTemplate>
- </FooterTemplate>
- </asp:Repeater>
Now, add the code given below in the back-end file.
- //initialize data class
- var db = new CartDataClassesDataContext();
- //Setting data from database
- rptCartItem.DataSource = db.tblCarts.ToList();
- rptCartItem.DataBind();
- //To call javascript function from code behind
- Page.ClientScript.RegisterStartupScript(this.GetType(),"RecalculateRepeater","RecalculateRepeater()",true);
Add the label for Total Price, Discount Amount, and Shipping, Amount To Pay.
- <span style = "padding-left:300px" > Your Cart Total Value: < /span>
- <asp: Label ID = "lblTotalPrice"
- style = "padding-left:45px"
- runat = "server"
- ClientIDMode = "Static" > < /asp:Label>
- <br / >
- <span style = "padding-left:370px" > (-) Discount: < /span>
- <asp: Label ID = "lblDiscountAmount"
- style = "padding-left:52px"
- runat = "server"
- ClientIDMode = "Static"
- Text = "1000" > < /asp:Label>
- <br / >
- <span style = "padding-left:368px" > (+) Shipping: < /span>
- <asp: Label ID = "lblShipping"
- style = "padding-left:55px"
- runat = "server"
- ClientIDMode = "Static"
- Text = "500" > < /asp:Label>
- <br / >
- <br / >
- <hr / >
- <span style = "padding-left:345px" > Amount To Pay: < /span>
- <asp: Label ID = "lblAmounttoPay"
- style = "padding-left:40px"
- runat = "server"
- ClientIDMode = "Static" > < /asp:Label>
The javaScript function is at the heart of this article. Let's understand the commands given below.


Join the conversation! Your thoughts help the community grow.