Videos | Podcasts

Repeater Paging using JavaScript
Agha Usman Ahmed
Published Date: 2/25/2009 12:18:25 PM
Views: 2730

Abstract:
In this article we will guide you about Repeater Paging using JavaScript. Actually, paging is something in which you rendered selected records on the page either from the database or the data table. But in some scenario we cannot afford the post back operation on every page change. So this article explains you how to do paging using JavaScript. Before we start it should be clear that this is not AJAX it is simple JavaScript which makes repeater control page able.

Implementation:

So here we go with our Repeater Control which can be bound using any data source but we are using LinqDatasource.


<asp:Repeater ID="Repeater1" runat="server" DataSourceID="LinqDataSource1">
        <HeaderTemplate>
            <div id="pgInfo">Page X of Z</div>
            <div id="Paging"><a href="javascript:void(0);" onclick="movePrevious()">Previous</a><div id="pageNumbers" style="display:inline"></div>
        <a href="javascript:void(0);" onclick="moveNext()">Next</a></div>
                
            <div id="dvRecords">
        
        </HeaderTemplate>
        <ItemTemplate>
            <div class="pageable" style="border:solid 1px blue;padding:2px;height:auto"><%#Eval("Product_Name") %><br /><%#Eval("Product_Detail") %></div>
        </ItemTemplate>
        <FooterTemplate>
        </div>
        <script>
            managePaging();
        </script>
        </FooterTemplate>
    </asp:Repeater>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server"
        ContextTypeName="LearnWebApp.MyDbDataContext" TableName="Tbl_Products">
    </asp:LinqDataSource>


In header section we have created three divs one is for Page Index Display and another is to hold the paging liks and the last one is the container of all the records we will rendered in ItemTemplate.

In Item Template section we have created a div which hold the records and the important part to mention is the class “pageable” notice if we use any div in our ItemTemplate which has no class or any class other then “pageable” it will not be processed for paging by our JavaScript. 

And in the Footer section we are simple calling a function called managePaging. This function will set the whole paging stuff once the rendering of page able items is complete. That is why we have call this function in footer section.


<script language="javascript">
    var pgPrefix = "pg"; // Thats is for Div id as we don't want to pass number in div Id
    var currPage = 1; // Index of the current page
    var recCount = 1; // record count, will be set by the functions
    var pgSize= 3; //this is the size of the page which can be changed according to the need
    var pgCount = 1; // page count, will be set by the functions
    function managePageIndex()
    {
        var strIndexText = "Page " + currPage + " of " + pgCount;
        document.getElementById("pgInfo").innerHTML = strIndexText;//in some cases you need to use textContent for mozila but that is rare.
    }
    function managePageNumbers()
    {
        var obj = document.getElementById("pageNumbers");
        obj.innerHTML = "";
        
        for (i=1;i<=pgCount;i++)
        {
            obj.innerHTML += "<a href='javascript:void(0);' onclick='showPage(" + i + ")'>" + i + "</a> ";
        }
        
    }
    function managePaging()
    {
        var obj = document.getElementById("dvRecords");
        var divs = obj.getElementsByTagName("div");
        
        
        for(i=0;i<=divs.length -1;i++)
        {
            if (recCount > pgSize)
            {
                pgCount++;
                recCount = 0;
            }
            
            var pgName = pgPrefix + pgCount; // Create div name as per current page
            if (divs[i].className != null && divs[i].className == "pageable")
            {
                divs[i].setAttribute("name",pgName);
            }
            recCount++;
        }
        managePageNumbers();
        showPage(currPage);
        
    }
    
    function showPage(pageNum)
    {
        var pgName = pgPrefix + pageNum;
        var obj = document.getElementById("dvRecords");
        var divs = obj.getElementsByTagName("div");
        currPage = pageNum;
        for(i=0;i<=divs.length -1;i++)
        {
            if (divs[i].getAttribute("name") == pgName)
            {
                divs[i].style.display = "block";
            }
            else
            {
                divs[i].style.display = "none";
            }
        }
        managePageIndex();
    }
function moveNext()
    {
        if ((currPage + 1) > pgCount)
        {
            currPage = 1;
        }
        else
        {
            currPage++;
        }
        showPage(currPage);
    }
    
    function movePrevious()
    {
        if ((currPage - 1) < 1)
        {
            currPage = pgCount;
        }
        else
        {
            currPage--;
        }
        
        showPage(currPage);
        
    }

    </script>



As we can see we have taken some global variables which are in used by multiple functions for the purpose of page index and count settings.  Now read the following description of each function:

managePageIndex : This function is used to set the Page Index Display.
managePageNumbers: This function is used to get the total number of pages and display it accordingly.
managePaging : This is the key function, it will read all the divs inside the main div (remember we have created a container div to hold all the records) and  set the name attribute of inner div according to the page index and call the show page method with the current index.
showPage :  This function will hide all the divs except the who’s page index is  specified in the parameter.
moveNext and movePrevious : Both of these functions are off course use to move the page index to next and previous.

Conclusion:

In this article we learned how to perform the Repeater control paging using JavaScript.



Did you like this article?
kick it on DotNetKicks.com Submit
Similar Articles

Displaying Custom Message When Nested Repeator Contains No Items

Nested Repeaters to Display Nested Data

Video Tutorial (FEEDBACK NEEDED)

Getting Only Changes from DataGrid

Accessing Different Controls inside the Datagrid

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks