Click here to Skip to main content
15,886,788 members
Articles / Web Development / ASP.NET
Tip/Trick

Implement "Load More" in ASP.NET Grid

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Apr 2013CPOL1 min read 18.8K   478   6   1
Create an infinite list with “Load More” button appearing at the bottom of the lists that can be used to load data on demand, using AJAX
Image 1

Implement “Load More” in ASP.NET Grid

Create an infinite list with “Load More” button appearing at the bottom of the lists that can be used to load data on demand, using AJAX.

Background

The idea is to have a Load More in GridView, like we have this feature implemented in most mobile applications like Facebook and Twitter.

Using the Code

In order to show you the entire pattern and to capture each and every detail of it, I will break it down into the following tasks:

  • Step 1: Place a GridView in an UpdatePanel and bind it to SQLDataSource control that will pull NorthWind customers information.
  • Step 2: Add a Select parameter to SQLDataSource which will allow me to show 10 rows during initial load. Then I’ll add a “Load More” button to the page which will load additional 10 rows and add to the grid.
  • Step 3: Finally, I will set the button control as the update trigger for the UpdatePanel so that the user doesn’t lose the scroll point when loading rows and UpdateProgress to show a progress indication when the request is in progress.

Step 1: Create Webpage

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebGrid.ascx.cs" Inherits="WebGrid" %>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:GridView ID="gView" runat="server" Width="646px" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
       <Columns>
       <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" />
       <asp:BoundField DataField="CompanyName" HeaderText="Company Name" />
       <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
       <asp:BoundField DataField="ContactTitle" HeaderText="Contact Title" />
       <asp:BoundField DataField="Country" HeaderText="Country" />
       </Columns>
    </asp:GridView>
    </ContentTemplate>
    <Triggers>
            <asp:AsyncPostBackTrigger ControlID="LoadMore" EventName="Click">
            </asp:AsyncPostBackTrigger>
        </Triggers>
</asp:UpdatePanel>
<asp:Button ID="LoadMore" runat="server" Height="28px" onclick="LoadMore_Click" 
    Text="Load More..." Width="648px" />

      <asp:UpdateProgress ID="UpdateProgress1" runat="server" 
    ClientIDMode="Static">
    <ProgressTemplate>
        <b>Loading Additional Data....</b>
    </ProgressTemplate>
</asp:UpdateProgress>

Step 2

ASP.NET
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="select top(@TopVal) * from Customers" >
    <SelectParameters>
        <asp:Parameter Name="TopVal" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Step 3

Add the code for the Button click event.

C#
protected void LoadMore_Click(object sender, EventArgs e)
{
    this.SqlDataSource1.SelectParameters["TopVal"].DefaultValue = 
       (int.Parse(this.SqlDataSource1.SelectParameters["TopVal"].DefaultValue) + 10).ToString();
}

Points of Interest

The code can be converted to a web control having facility to set the pageSize.

Create a property for page size:

Step 1

Add a private variable:

C#
private int _PageSize;

Step 2

Create a property for page size.

Step 3

C#
public int PageSize
{
    get {return _PageSize; }
    set { _PageSize = value; }
}

Change the button click method:

C#
protected void LoadMore_Click(object sender, EventArgs e)
{
    this.SqlDataSource1.SelectParameters["TopVal"].DefaultValue = 
      (int.Parse(this.SqlDataSource1.SelectParameters["TopVal"].DefaultValue) + PageSize).ToString();
}

Step 4

Add the below code to page load:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.SqlDataSource1.SelectParameters["TopVal"].DefaultValue += PageSize;
    }
}

Usage

XML
<uc1:WebGrid ID="WebGrid1" PageSize="5"  runat="server"/>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer www.bhargavapandey.com
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRemove Load More button Pin
Member 1038534117-Nov-13 15:07
Member 1038534117-Nov-13 15:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.