When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, August 29, 2001

Custom ASP.NET Datagrid Paging With Exact Count, Part 2
By Dimitrios Markatos


  • Read Part 1

  • In Part 1 we looked at creating a custom paging solution using ASP.NET's DataGrid Web control. In Part 1 we focused in on the first (and most detailed) server-side subroutine: BindSQL(). In this part we'll look at the HTML content of the custom paging Web page and examine the other server-side subroutines and event handlers.

    - continued -

    The HTML Portion
    In our HTML section we need a couple of label controls to display various bits of information, such as what page we are currently viewing, how many total records there are, etc. We also need a DataGrid, to which we are binding the database results to. Don't forget to put the DataGrid in a WebForm (a <form> tag with the runat="server" attribute specified). Finally, we need a series of LinkButton Web controls, to display our Prev/First Page and Next/Last Page links.

    The content for the HTML section is as follows:

    <html>
    <body>
      'For our recordcount and pagecount
      <asp:Label id="lblPageCount" runat="server" /><br>
      <asp:label id="RecordCount" runat="server" />
      
      <form runat="server">
        <ASP:Datagrid id="pubs" runat="server" 
                      AllowPaging="True" 
                      AllowCustomPaging="False" 
                      Pagesize="10" 
                      PagerStyle-Visible = "False"
        />
    
        <%-- Display the First Page/Previous Page buttons --%>
        <asp:linkbutton id="Firstbutton" Text="<< 1st Page" 
                        CommandArgument="0" runat="server" 
                        onClick="PagerButtonClick"/>
        <asp:linkbutton id="Prevbutton" Text= "" 
                        CommandArgument="prev" runat="server" 
                        onClick="PagerButtonClick"/>
          
    
        <%-- Display the Next Page/Last Page buttons --%>
        <asp:linkbutton id="Nextbutton" Text= "" 
                        CommandArgument="next" runat="server" 
                        onClick="PagerButtonClick"/>
        <asp:linkbutton id="Lastbutton" Text="Last Page >>" 
                        CommandArgument="last" runat="server" 
                        onClick="PagerButtonClick"/>
    
        <br><br><br><br>
    
        Change Pagesize
        <asp:DropDownList id="ps" runat="server">
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
            <asp:ListItem selected>10</asp:ListItem>
            <asp:ListItem>12</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>22</asp:ListItem>
        </asp:DropDownList>
        
        <asp:button text="Change Pagesize" runat="server" OnClick="RePage"/>
      </form>
    </body>
    </html>
    

    Note that in our DataGrid Web control we set the PagerStyle's Visible property to False. This is because we are implementing our own paging solution, and don't want to use the default paging style supported by the DataGrid Web control. (For more information on paging database results using the DataGrid's built-in functionality, be sure to read: Paging Database Results in ASP.NET!) Also note that the four LinkButton controls all specify the server-side subroutine PagerButtonClick as the sub to be called when they are clicked; similarly, the "Change Pagesize" button has the RePage subroutine defined as its OnClick event handler.

    The event handler for the four LinkButtons (PagerButtonClick) must display the appropriate page of data, be it the next page, the previous page, the first page, or the last page. Which page to display, of course, depends on what LinkButton the user clicked. The PagerButtonClick (shown below) uses the CommandArgument passed in from the LinkButton Web controls to determine which control was clicked, and then takes the appropriate action.

    Sub PagerButtonClick(sender As Object, e As EventArgs)
      'used by external paging UI
      Dim arg As String = sender.CommandArgument
    
      Select arg
         Case "next":  'The next Button was Clicked
            If (Pubs.CurrentPageIndex < (Pubs.PageCount - 1)) Then
                Pubs.CurrentPageIndex += 1
            End If 
    
         Case "prev":   'The prev button was clicked
             If (Pubs.CurrentPageIndex > 0) Then
                 Pubs.CurrentPageIndex -= 1
             End If
    
         Case "last":   'The Last Page button was clicked
             Pubs.CurrentPageIndex = (Pubs.PageCount - 1)
    
         Case Else:     'The First Page button was clicked
             Pubs.CurrentPageIndex = Convert.ToInt32(arg)
    	End Select
    
        'Now, bind the data!
        BindSQL()
    End Sub
    

    The RePage event handler, which is called when the "Change Pagesize" button is clicked, simply resets the DataGrid's CurrentPageIndex property back to 0 and rebinds the database data:

    Sub Repage(sender As Object, e As EventArgs)
      Pubs.CurrentPageIndex = 0
      BindSQL()
    End Sub
    

    Finally, the last two server-side subroutines are two meager helper subroutines, Next_Buttons() and Prev_Buttons(), which display the correct text for each of the LinkButtons. These two subs, which are called from BindSQL(), can be seen below:

    Sub Prev_Buttons()
      Dim PrevSet As String
    
      If Pubs.CurrentPageIndex+1 <> 1 and ResultCount <> -1 Then
        PrevSet = Pubs.PageSize
        PrevButton.Text = ("< Prev " & PrevSet)
    	
        If Pubs.CurrentPageIndex+1 = Pubs.PageCount Then
          FirstButton.Text = ("<< 1st Page")
        End If
      End If
    End Sub
    
    
    Sub Next_Buttons()
      Dim NextSet As String
    
      If Pubs.CurrentPageIndex+1 < Pubs.PageCount Then
        NextSet = Pubs.PageSize
        NextButton.Text = ("Next " & NextSet & " >")
      End If
    
      If Pubs.CurrentPageIndex+1 = Pubs.PageCount-1 Then
        Dim EndCount As Integer 
        EndCount = ResultCount - (Pubs.PageSize * (Pubs.CurrentPageIndex+1))
        NextButton.Text = ("Next " & EndCount & " >")
      End If
    End Sub
    
    [View a live demo!]

    Well, that's it! Be sure to view the complete code, try out the live demo, and read up on the related articles! If you have any questions, please do not hesitate to email me!

    Happy Programming!

  • By Dimitrios Markatos


    Attachments:

  • View the live demo!
  • Read Paging Database Results in ASP.NET
  • View the complete source (in text format)


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES