Hi Tevin,
Check this example. Now please take its reference and correct your code.
HTML
<table class="table table-striped table-bordered table-hover table-condensed" border="1">
<tr>
<td style="padding-bottom: 10px">Part Weight:<br />
<asp:TextBox ID="txtPartWeight" runat="server" />
</td>
<td style="padding-bottom: 10px">Sprue Weight:<br />
<asp:TextBox ID="txtSprueWeight" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="center" style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
<asp:Repeater ID="rpAddedWights" runat="server">
<HeaderTemplate>
<table class="table table-striped table-bordered table-hover table-condensed" border="1">
<thead>
<tr>
<th>Part Weight (g)</th>
<th>Sprue Weight (g)</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label Text='<%#DataBinder.Eval(Container.DataItem, "PartWeight")%>' runat="server" ID="lblPartWeight" /></td>
<td>
<asp:Label Text='<%#DataBinder.Eval(Container.DataItem, "SprueWeight")%>' runat="server" ID="lblSprueWeight" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
<tfoot>
<tr>
<td>
<asp:Label ID="lblFPartWeight" runat="server"></asp:Label></td>
<td>
<asp:Label ID="lblFSprueWeight" runat="server"></asp:Label></td>
</tr>
</tfoot>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("PartWeight"), new DataColumn("SprueWeight") });
ViewState["Weights"] = dt;
this.BindRepeater();
}
}
protected void BindRepeater()
{
DataTable dt = (DataTable)ViewState["Weights"];
if (dt.Rows.Count > 0)
{
rpAddedWights.DataSource = dt;
rpAddedWights.DataBind();
var partWeight = dt.AsEnumerable().Select(x => Convert.ToDecimal(x["PartWeight"])).ToList().AsQueryable();
var sprueWeight = dt.AsEnumerable().Select(x => Convert.ToDecimal(x["SprueWeight"])).ToList().AsQueryable();
decimal averagePart = Queryable.Average(partWeight);
decimal averageSprue = Queryable.Average(sprueWeight);
(rpAddedWights.Controls[rpAddedWights.Controls.Count - 1].Controls[0].FindControl("lblFPartWeight") as Label).Text = "Average : <br/>" + Math.Round(averagePart, 2).ToString();
(rpAddedWights.Controls[rpAddedWights.Controls.Count - 1].Controls[0].FindControl("lblFSprueWeight") as Label).Text = "Average : <br/>" + Math.Round(averageSprue, 2).ToString();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["Weights"];
if (dt.Rows.Count < 5)
{
dt.Rows.Add(txtPartWeight.Text.Trim(), txtSprueWeight.Text.Trim());
ViewState["Customers"] = dt;
this.BindRepeater();
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('You have reached the limit of rows added.')", true);
}
txtPartWeight.Text = string.Empty;
txtSprueWeight.Text = string.Empty;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("PartWeight"), New DataColumn("SprueWeight")})
ViewState("Weights") = dt
Me.BindRepeater()
End If
End Sub
Protected Sub BindRepeater()
Dim dt As DataTable = CType(ViewState("Weights"), DataTable)
If dt.Rows.Count > 0 Then
rpAddedWights.DataSource = dt
rpAddedWights.DataBind()
Dim partWeight = dt.AsEnumerable().Select(Function(x) Convert.ToDecimal(x("PartWeight"))).ToList().AsQueryable()
Dim sprueWeight = dt.AsEnumerable().Select(Function(x) Convert.ToDecimal(x("SprueWeight"))).ToList().AsQueryable()
Dim averagePart As Decimal = Queryable.Average(partWeight)
Dim averageSprue As Decimal = Queryable.Average(sprueWeight)
TryCast(rpAddedWights.Controls(rpAddedWights.Controls.Count - 1).Controls(0).FindControl("lblFPartWeight"), Label).Text = "Average : <br/>" & Math.Round(averagePart, 2).ToString()
TryCast(rpAddedWights.Controls(rpAddedWights.Controls.Count - 1).Controls(0).FindControl("lblFSprueWeight"), Label).Text = "Average : <br/>" & Math.Round(averageSprue, 2).ToString()
End If
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("Weights"), DataTable)
If dt.Rows.Count < 5 Then
dt.Rows.Add(txtPartWeight.Text.Trim(), txtSprueWeight.Text.Trim())
ViewState("Customers") = dt
Me.BindRepeater()
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('You have reached the limit of rows added.')", True)
End If
txtPartWeight.Text = String.Empty
txtSprueWeight.Text = String.Empty
End Sub
Screenshot