Hi nauna,
Refer below sample.
HTML
<asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
<ItemTemplate>
<div class="pickup">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Remove">Remove</asp:LinkButton><br />
</div>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
<asp:Button Text="Save" runat="server" OnClick="Button1_Click" />
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int rowID;
if (e.Item.DataItemIndex == 0 || e.Item.DataItemIndex == ListView1.Items.Count - 1)
{
rowID = ListView1.Items[e.Item.DataItemIndex].DataItemIndex;
}
else
{
rowID = ListView1.Items[e.Item.DataItemIndex + 1].DataItemIndex;
}
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (ListView1.SelectedIndex < dt.Rows.Count - 1)
{
dt.Rows.Remove(dt.Rows[rowID]);
}
}
ViewState["CurrentTable"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();
}
SetPreviousData();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
ListView1.DataSource = dt;
ListView1.DataBind();
}
private void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox1");
TextBox box2 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox2");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
ListView1.DataSource = dtCurrentTable;
ListView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox1");
TextBox box2 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox2");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
protected void Button1_Click(object sender, EventArgs e)
{
string message = "";
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox1");
TextBox box2 = (TextBox)ListView1.Items[rowIndex].FindControl("TextBox2");
rowIndex++;
message += box1.Text + "," + box2.Text + "\\n\\r";
}
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('" + message + "');", true);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
Dim rowID As Integer
If e.Item.DataItemIndex = 0 OrElse e.Item.DataItemIndex = ListView1.Items.Count - 1 Then
rowID = ListView1.Items(e.Item.DataItemIndex).DataItemIndex
Else
rowID = ListView1.Items(e.Item.DataItemIndex + 1).DataItemIndex
End If
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 1 Then
If ListView1.SelectedIndex < dt.Rows.Count - 1 Then
dt.Rows.Remove(dt.Rows(rowID))
End If
End If
ViewState("CurrentTable") = dt
ListView1.DataSource = dt
ListView1.DataBind()
End If
SetPreviousData()
End Sub
Private Sub SetInitialRow()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Column1", GetType(String)))
dt.Columns.Add(New DataColumn("Column2", GetType(String)))
dr = dt.NewRow()
dr("RowNumber") = 1
dr("Column1") = String.Empty
dr("Column2") = String.Empty
dt.Rows.Add(dr)
ViewState("CurrentTable") = dt
ListView1.DataSource = dt
ListView1.DataBind()
End Sub
Private Sub AddNewRow()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox2"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = i + 1
drCurrentRow("Column1") = box1.Text
drCurrentRow("Column2") = box2.Text
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
ListView1.DataSource = dtCurrentTable
ListView1.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 1 To dt.Rows.Count - 1
Dim box1 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox2"), TextBox)
box1.Text = dt.Rows(i)("Column1").ToString()
box2.Text = dt.Rows(i)("Column2").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
AddNewRow()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = ""
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox1"), TextBox)
Dim box2 As TextBox = CType(ListView1.Items(rowIndex).FindControl("TextBox2"), TextBox)
rowIndex += 1
message += box1.Text & "," + box2.Text & "\n\r"
Next
Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('" & message & "');", True)
End If
End If
End Sub
Screenshot