Hi Tevin,
Check this example. Now please take its reference and correct your code.
HTML
<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>
</table>
</FooterTemplate>
</asp:Repeater>
<hr />
<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>
</tr>
<tr>
<td style="padding-bottom: 10px">Sprue Weight:<br />
<asp:TextBox ID="txtSprueWeight" runat="server" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
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()
{
rpAddedWights.DataSource = (DataTable)ViewState["Weights"];
rpAddedWights.DataBind();
}
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()
rpAddedWights.DataSource = CType(ViewState("Weights"), DataTable)
rpAddedWights.DataBind()
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