ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Refresh ListView after insert record to Database using jQuery AJAX in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <p><strong>Login.aspx</strong></p> <pre class="brush: html">&lt;asp:Login ID=&#34;Login1&#34; runat=&#34;server&#34; OnAuthenticate=&#34;Login1_Authenticate&#34;&gt; &lt;/asp:Login&gt;</pre> <p><strong>Login.aspx.cs</strong></p> <pre class="brush: csharp">protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { // Write code for login validation. FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet); }</pre> <p><strong>Login.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs) FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet) End Sub</pre> <p><strong>Home.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.Page.User.Identity.IsAuthenticated) { FormsAuthentication.RedirectToLoginPage(); } else { Response.Redirect(&#34;~/CS.aspx?Country=India&#34;); } }</pre> <p><strong>Home.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.Page.User.Identity.IsAuthenticated Then FormsAuthentication.RedirectToLoginPage() Else Response.Redirect(&#34;~/CS.aspx?Country=India&#34;) End If End Sub</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;div&gt; &lt;asp:ListView ID=&#34;lvCustomers&#34; runat=&#34;server&#34; GroupPlaceholderID=&#34;groupPlaceHolder1&#34; ItemPlaceholderID=&#34;itemPlaceHolder1&#34;&gt; &lt;LayoutTemplate&gt; &lt;table cellpadding=&#34;0&#34; id=&#34;tblCustomers&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;th&gt; CustomerId &lt;/th&gt; &lt;th&gt; Name &lt;/th&gt; &lt;th&gt; Country &lt;/th&gt; &lt;/tr&gt; &lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;groupPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/table&gt; &lt;/LayoutTemplate&gt; &lt;GroupTemplate&gt; &lt;tr&gt; &lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;itemPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/tr&gt; &lt;/GroupTemplate&gt; &lt;ItemTemplate&gt; &lt;td class=&#34;CustomerId&#34;&gt; &lt;%# Eval(&#34;CustomerId&#34;) %&gt; &lt;/td&gt; &lt;td class=&#34;Name&#34;&gt; &lt;%# Eval(&#34;Name&#34;) %&gt; &lt;/td&gt; &lt;td class=&#34;Country&#34;&gt; &lt;%# Eval(&#34;Country&#34;) %&gt; &lt;/td&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Insert&#34; ID=&#34;btnInsert&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { BindListView(); $(&#39;#btnInsert&#39;).on(&#39;click&#39;, function () { var user = {}; user.name = &#34;&lt;%= HttpContext.Current.User.Identity.Name%&gt;&#34;; // Get UserName from Identity. user.country = window.location.search.split(&#39;=&#39;)[1]; // Get QueryString Value. $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/InsertCustomer&#34;, data: JSON.stringify(user), contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (response) { if (response.d == &#34;Success&#34;) { BindListView(); } else { alert(response.d); } }, failuer: function (response) { alert(response.d); }, error: function (response) { alert(response.responseText); } }); return false; }); }); function BindListView() { $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetCustomers&#34;, data: {}, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: OnSuccess, failuer: function (response) { alert(resposnse.d); }, error: function (response) { alert(resposnse.responseText); } }); }; function OnSuccess(response) { var customers = response.d; var row = $(&#34;[id*=tblCustomers] tr:last-child&#34;).clone(true); var row1 = $(&#34;[id*=tbl1Customers] tr:last-child&#34;).clone(true); $(&#34;[id*=tblCustomers] tr&#34;).not($(&#34;[id*=tblCustomers] tr:first-child&#34;)).remove(); $(customers).each(function () { var customer = $(this)[0]; $(&#34;.CustomerId&#34;, row).html(customer.Name); $(&#34;.Name&#34;, row).html(customer.Name); $(&#34;.Country&#34;, row).html(customer.Country); $(&#34;[id*=tblCustomers]&#34;).append(row); row = $(&#34;[id*=tblCustomers] tr:last-child&#34;).clone(true); }); } &lt;/script&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Data.SqlClient; using System.Configuration; using System.Web.Services; using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Data.SqlClient Imports System.Configuration Imports System.Web.Services Imports System.Data </pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { // To Bind Dummy data to listView To get clone on Jquery OnSuccess method. this.BindDummyRow(); } } private void BindDummyRow() { DataTable dummy = new DataTable(); dummy.Columns.Add(&#34;CustomerId&#34;); dummy.Columns.Add(&#34;Name&#34;); dummy.Columns.Add(&#34;Country&#34;); dummy.Rows.Add(); lvCustomers.DataSource = dummy; lvCustomers.DataBind(); } [WebMethod] public static List&lt;Customer&gt; GetCustomers() { string constr = ConfigurationManager.ConnectionStrings[&#34;ConStr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers&#34;); cmd.CommandType = CommandType.Text; cmd.Connection = con; List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { customers.Add(new Customer { CustomerId = sdr[&#34;CustomerId&#34;].ToString(), Name = sdr[&#34;Name&#34;].ToString(), Country = sdr[&#34;Country&#34;].ToString() }); } con.Close(); return customers; } [WebMethod] public static string InsertCustomer(string name, string country) { string constr = ConfigurationManager.ConnectionStrings[&#34;ConStr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;INSERT INTO Customers VALUES(@Name,@Country)&#34;); cmd.CommandType = CommandType.Text; cmd.Connection = con; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name); cmd.Parameters.AddWithValue(&#34;@Country&#34;, country); con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); return i &gt; 0 ? &#34;Success&#34; : &#34;Fail&#34;; } public class Customer { public string CustomerId { get; set; } public string Name { get; set; } public string Country { get; set; } }</pre> <p><strong>Default.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Me.BindDummyRow() End If End Sub Private Sub BindDummyRow() Dim dummy As DataTable = New DataTable() dummy.Columns.Add(&#34;CustomerId&#34;) dummy.Columns.Add(&#34;Name&#34;) dummy.Columns.Add(&#34;Country&#34;) dummy.Rows.Add() lvCustomers.DataSource = dummy lvCustomers.DataBind() End Sub &lt;WebMethod&gt; Public Shared Function GetCustomers() As List(Of Customer) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;ConStr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers&#34;) cmd.CommandType = CommandType.Text cmd.Connection = con Dim customers As List(Of Customer) = New List(Of Customer)() con.Open() Dim sdr As SqlDataReader = cmd.ExecuteReader() While sdr.Read() customers.Add(New Customer With { .CustomerId = sdr(&#34;CustomerId&#34;).ToString(), .Name = sdr(&#34;Name&#34;).ToString(), .Country = sdr(&#34;Country&#34;).ToString() }) End While con.Close() Return customers End Function &lt;WebMethod&gt; Public Shared Function InsertCustomer(ByVal name As String, ByVal country As String) As String Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;ConStr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO Customers VALUES(@Name,@Country)&#34;) cmd.CommandType = CommandType.Text cmd.Connection = con cmd.Parameters.AddWithValue(&#34;@Name&#34;, name) cmd.Parameters.AddWithValue(&#34;@Country&#34;, country) con.Open() Dim i As Integer = cmd.ExecuteNonQuery() con.Close() Return If(i &gt; 0, &#34;Success&#34;, &#34;Fail&#34;) End Function Public Class Customer Public Property CustomerId As String Public Property Name As String Public Property Country As String End Class</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/ParbeQc.gif" alt="" width="374" height="297" /></p>https://www.aspforums.net:443/Threads/476712/Refresh-ListView-after-insert-record-to-Database-using-jQuery-AJAX-in-ASPNet/https://www.aspforums.net:443/Threads/476712/Refresh-ListView-after-insert-record-to-Database-using-jQuery-AJAX-in-ASPNet/Mon, 30 Jul 2018 05:58:01 GMT