Hi nauna,
Refer below sample.
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&key=API_KEY"></script>
<script type="text/javascript">
$(function () {
$("div[class=loadpost]").each(function () {
var textbox1 = $(this).find('[id*=TextBox1]');
var textbox2 = $(this).find('[id*=TextBox2]');
ApplyAutoComplete(textbox1);
ApplyAutoComplete(textbox2);
});
});
function ApplyAutoComplete(input) {
google.maps.event.addDomListener(window, 'load', function () {
var places;
for (var i = 0; i < input.length; i++) {
var options = { types: ['(regions)'] };
places = new google.maps.places.Autocomplete(input[i], options);
}
google.maps.event.addListener(places, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var mesg = "Address: " + address;
});
});
}
</script>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<div class="loadpost">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="Button1" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
<br />
<asp:Label ID="lblduration" runat="server" />
<br />
<asp:Label ID="lbldistance" runat="server" />
Namespaces
C#
using System.Data;
using System.Net;
using System.IO;
using System.Text;
VB.Net
Imports System.Data
Imports System.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
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)));
dt.Columns.Add(new DataColumn("Distance", typeof(int)));
dt.Columns.Add(new DataColumn("Duration", typeof(int)));
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();
string origin = "";
string destination = "";
DataTable dt = (DataTable)ViewState["CurrentTable"];
origin = dt.Rows[dt.Rows.Count - 1]["Column1"].ToString();
destination = dt.Rows[dt.Rows.Count - 1]["Column2"].ToString();
string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + origin + "&destinations=" + destination + "&key=API_KEY";
WebRequest request = WebRequest.Create(url);
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
DataSet dsResult = new DataSet();
dsResult.ReadXml(reader);
dt.Rows[dt.Rows.Count - 1]["Distance"] = dsResult.Tables["Distance"].Rows[0]["value"].ToString();
dt.Rows[dt.Rows.Count - 1]["Duration"] = dsResult.Tables["Duration"].Rows[0]["value"].ToString();
}
}
ViewState["CurrentTable"] = dt;
int sumDistance = Convert.ToInt32(dt.Compute("SUM(Distance)", string.Empty));
int sumDuration = Convert.ToInt32(dt.Compute("SUM(duration)", string.Empty));
lblduration.Text = "Duration is : " + sumDuration / 60 + " min";
lbldistance.Text = "Distance is : " + sumDistance / 1000 + " km";
}
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
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)))
dt.Columns.Add(New DataColumn("Distance", GetType(Integer)))
dt.Columns.Add(New DataColumn("Duration", GetType(Integer)))
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()
Dim origin As String = ""
Dim destination As String = ""
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
origin = dt.Rows(dt.Rows.Count - 1)("Column1").ToString()
destination = dt.Rows(dt.Rows.Count - 1)("Column2").ToString()
Dim url As String = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & origin & "&destinations=" & destination & "&key=API_KEY"
Dim request As WebRequest = WebRequest.Create(url)
Using response As WebResponse = CType(request.GetResponse(), HttpWebResponse)
Using reader As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.UTF8)
Dim dsResult As DataSet = New DataSet()
dsResult.ReadXml(reader)
dt.Rows(dt.Rows.Count - 1)("Distance") = dsResult.Tables("Distance").Rows(0)("value").ToString()
dt.Rows(dt.Rows.Count - 1)("Duration") = dsResult.Tables("Duration").Rows(0)("value").ToString()
End Using
End Using
ViewState("CurrentTable") = dt
Dim sumDistance As Integer = Convert.ToInt32(dt.Compute("SUM(Distance)", String.Empty))
Dim sumDuration As Integer = Convert.ToInt32(dt.Compute("SUM(duration)", String.Empty))
lblduration.Text = "Duration is : " & sumDuration / 60 & " min"
lbldistance.Text = "Distance is : " & sumDistance / 1000 & " km"
End Sub
Screenshot