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.Load data on Page Scroll from Session (Without Database) using jQuery Ajax in ASP.Net<p>Hi <a class="username" rel="Poonam123">Poonam123</a>,</p> <p>Refer below sample.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx" target="_blank">Download Northwind Database</a></p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;table class=&#34;Grid&#34; cellspacing=&#34;0&#34; rules=&#34;all&#34; border=&#34;1&#34; id=&#34;Table1&#34; style=&#34;width: 500px; border-collapse: collapse;&#34;&gt; &lt;tr&gt; &lt;th scope=&#34;col&#34; style=&#34;width: 200px;&#34;&gt; Customer Name &lt;/th&gt; &lt;th scope=&#34;col&#34; style=&#34;width: 100px;&#34;&gt; City &lt;/th&gt; &lt;th scope=&#34;col&#34; style=&#34;width: 100px;&#34;&gt; Country &lt;/th&gt; &lt;th scope=&#34;col&#34; style=&#34;width: 100px;&#34;&gt; Postal Code &lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div id=&#34;dvGrid&#34; style=&#34;height: 250px; overflow: auto; width: 517px&#34;&gt; &lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; CssClass=&#34;Grid&#34; Width=&#34;500&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;Customer Name&#34; ItemStyle-CssClass=&#34;name&#34; ItemStyle-Width=&#34;200&#34; HeaderStyle-Width=&#34;200&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; ItemStyle-CssClass=&#34;city&#34; ItemStyle-Width=&#34;100&#34; HeaderStyle-Width=&#34;100&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; ItemStyle-CssClass=&#34;country&#34; ItemStyle-Width=&#34;100&#34; HeaderStyle-Width=&#34;100&#34; /&gt; &lt;asp:BoundField DataField=&#34;PostalCode&#34; HeaderText=&#34;Postal Code&#34; ItemStyle-CssClass=&#34;postal&#34; ItemStyle-Width=&#34;100&#34; HeaderStyle-Width=&#34;100&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;script src=&#34;jquery.min.js&#34; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var pageIndex = 1; var pageCount = &lt;%=this.PageCount %&gt;; $(function () { $(&#34;[id$=gvCustomers] tr&#34;).eq(0).remove(); }); $(&#34;#dvGrid&#34;).on(&#34;scroll&#34;, function (e) { var $o = $(e.currentTarget); if ($o[0].scrollHeight - $o.scrollTop() &lt;= $o.outerHeight()) { GetRecords(); } }); function GetRecords() { pageIndex++; if (pageIndex == 2 || pageIndex &lt;= pageCount) { if ($(&#34;[id$=gvCustomers] .loader&#34;).length == 0) { var row = $(&#34;[id$=gvCustomers] tr&#34;).eq(0).clone(true); row.addClass(&#34;loader&#34;); row.children().remove(); row.append(&#39;&lt;td colspan = &#34;999&#34; style = &#34;background-color:white&#34;&gt;&lt;img id=&#34;loader&#34; alt=&#34;&#34; src=&#34;103.gif&#34; /&gt;&lt;/td&gt;&#39;); $(&#34;[id$=gvCustomers]&#34;).append(row); } $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetCustomers&#34;, data: &#39;{pageIndex: &#39; + pageIndex + &#39;}&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: OnSuccess, error: function (response) { alert(response.d); } }); } else { return false; } } function OnSuccess(response) { var xmlDoc = $.parseXML(response.d); var xml = $(xmlDoc); var customers = xml.find(&#34;Table1&#34;); $(&#34;[id$=gvCustomers] .loader&#34;).remove(); customers.each(function () { var customer = $(this); var row = $(&#34;[id$=gvCustomers] tr&#34;).eq(0).clone(true); $(&#34;.name&#34;, row).html(customer.find(&#34;ContactName&#34;).text()); $(&#34;.city&#34;, row).html(customer.find(&#34;City&#34;).text()); $(&#34;.postal&#34;, row).html(customer.find(&#34;PostalCode&#34;).text()); $(&#34;.country&#34;, row).html(customer.find(&#34;Country&#34;).text()); $(&#34;[id$=gvCustomers]&#34;).append(row); }); $(&#34;#loader&#34;).hide(); } &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.Data; using System.Web.Services; using System.Configuration; using System.Data.SqlClient; using System.Linq; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Web.Services Imports System.Configuration Imports System.Data.SqlClient Imports System.Linq</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected int PageCount { get; set; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet ds = new DataSet(); int count = GetCustomersPageWise().Tables[0].Rows.Count / 10; int mod = GetCustomersPageWise().Tables[0].Rows.Count % 10; if (mod &gt; 0) { PageCount = count + 1; } else { PageCount = count; } ds.Tables.Add(GetCustomersPageWise().Tables[0].AsEnumerable().Skip(0).Take(10).CopyToDataTable()); gvCustomers.DataSource = ds; gvCustomers.DataBind(); } } public static DataSet GetCustomersPageWise() { string constring = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers ORDER BY CustomerID&#34;, con)) { using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { using (DataSet ds = new DataSet()) { sda.Fill(ds, &#34;Customers&#34;); HttpContext.Current.Session[&#34;ds&#34;] = ds; return ds; } } } } } [WebMethod] public static string GetCustomers(int pageIndex) { DataSet ds = HttpContext.Current.Session[&#34;ds&#34;] as DataSet; DataSet dsResult = new DataSet(); int startIndex = (pageIndex - 1) * 10; if ((ds.Tables[0].AsEnumerable().Skip(startIndex)).Count() &gt; 0) { dsResult.Tables.Add(ds.Tables[0].AsEnumerable().Skip(startIndex).Take(10).CopyToDataTable()); } return dsResult.GetXml(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Property PageCount As Integer Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load &nbsp;&nbsp;&nbsp; If Not IsPostBack Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ds As DataSet = New DataSet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim count As Integer = GetCustomersPageWise().Tables(0).Rows.Count / 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim [mod] As Integer = GetCustomersPageWise().Tables(0).Rows.Count Mod 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If [mod] &gt; 0 Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PageCount = count + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PageCount = count &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ds.Tables.Add(GetCustomersPageWise().Tables(0).AsEnumerable().Skip(0).Take(10).CopyToDataTable()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvCustomers.DataSource = ds &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvCustomers.DataBind() &nbsp;&nbsp;&nbsp; End If End Sub Public Shared Function GetCustomersPageWise() As DataSet &nbsp;&nbsp;&nbsp; Dim constring As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp; Using con As SqlConnection = New SqlConnection(constring) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers ORDER BY CustomerID&#34;, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using ds As DataSet = New DataSet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sda.Fill(ds, &#34;Customers&#34;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext.Current.Session(&#34;ds&#34;) = ds &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return ds &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp; End Using End Function &lt;WebMethod()&gt; Public Shared Function GetCustomers(ByVal pageIndex As Integer) As String &nbsp;&nbsp;&nbsp; Dim ds As DataSet = TryCast(HttpContext.Current.Session(&#34;ds&#34;), DataSet) &nbsp;&nbsp;&nbsp; Dim dsResult As DataSet = New DataSet() &nbsp;&nbsp;&nbsp; Dim startIndex As Integer = (pageIndex - 1) * 10 &nbsp;&nbsp;&nbsp; If (ds.Tables(0).AsEnumerable().Skip(startIndex).Count() &gt; 0) Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsResult.Tables.Add(ds.Tables(0).AsEnumerable().Skip(startIndex).Take(10).CopyToDataTable()) &nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; Return dsResult.GetXml() End Function</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=19f1c9ad-077a-42e2-87e7-a1938bd85041.png" alt="" width="555" height="411" /></p> <p>Note:</p> <pre class="brush: html">var pageCount = &lt;%=this.PageCount %&gt;;</pre> <p>Change the above line with below for VB.Net.</p> <pre class="brush: html">var pageCount = &lt;%=Me.PageCount %&gt;;</pre>https://www.aspforums.net:443/Threads/159432/Load-data-on-Page-Scroll-from-Session-Without-Database-using-jQuery-Ajax-in-ASPNet/https://www.aspforums.net:443/Threads/159432/Load-data-on-Page-Scroll-from-Session-Without-Database-using-jQuery-Ajax-in-ASPNet/Tue, 01 Oct 2019 02:17:25 GMT