You need to make use of ViewState and DataTable.
Add a row in DataTable with TextBox values keep the DataTable in ViewState and assign the DataTable to ListView as a DataSource.
Now when you again inserting the Item in ListView then get the data in DataTable from ViewState and Insert a new row to DataTable. Bind this DataTable to ListView.
This is a similar code which is implemented for GridView.
Code with ListView
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
.Grid
{
border: 1px solid #ccc;
margin-bottom: -1px;
}
.Grid th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
.Grid th, .Grid td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table cellpadding="0" cellspacing="0" class="Grid">
<tr>
<th>
Name
</th>
<th>
Country
</th>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Country") %>
</td>
</ItemTemplate>
</asp:ListView>
<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>
</div>
</form>
</body>
</html>
VB.Net Code
Namespace
Imports System.Data
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
ViewState("Customers") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
lvCustomers.DataSource = DirectCast(ViewState("Customers"), DataTable)
lvCustomers.DataBind()
End Sub
Protected Sub Insert(sender As Object, e As EventArgs)
Dim dt As DataTable = DirectCast(ViewState("Customers"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())
ViewState("Customers") = dt
Me.BindGrid()
txtName.Text = String.Empty
txtCountry.Text = String.Empty
End Sub
C# Code
Namespace
using System.Data;
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.BindGrid();
}
}
protected void BindGrid()
{
lvCustomers.DataSource = (DataTable)ViewState["Customers"];
lvCustomers.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.BindGrid();
txtName.Text = string.Empty;
txtCountry.Text = string.Empty;
}
Screenshot