Hi Tevin,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Repeater runat="server" ID="rptCustomers">
<HeaderTemplate>
<table>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label Text='<%# Eval("Name") %>' runat="server" /></td>
<td><asp:Label Text='<%# Eval("Country") %>' runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="padding-bottom: 10px">Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td style="padding-bottom: 10px">Country:<br />
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
Code
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("Name"), new DataColumn("Country") });
ViewState["Customers"] = dt;
this.BindRepeater();
}
}
protected void BindRepeater()
{
rptCustomers.DataSource = (DataTable)ViewState["Customers"];
rptCustomers.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["Customers"];
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim());
ViewState["Customers"] = dt;
this.BindRepeater();
txtName.Text = string.Empty;
txtCountry.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("Name"), New DataColumn("Country")})
ViewState("Customers") = dt
Me.BindRepeater()
End If
End Sub
Protected Sub BindRepeater()
rptCustomers.DataSource = CType(ViewState("Customers"), DataTable)
rptCustomers.DataBind()
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("Customers"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())
ViewState("Customers") = dt
Me.BindRepeater()
txtName.Text = String.Empty
txtCountry.Text = String.Empty
End Sub
Screenshot