ASP.NET
  Home arrow ASP.NET arrow Page 2 - Changing the Page Size Interactively in a ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Changing the Page Size Interactively in a DataGrid Web Control
By: Csaba Hatvany
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 25
    2003-03-31

    Table of Contents:
  • Changing the Page Size Interactively in a DataGrid Web Control
  • The Problem
  • The Solution
  • The Implementation
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Changing the Page Size Interactively in a DataGrid Web Control - The Problem


    (Page 2 of 5 )

    The DataGrid server control enables easy paging through data. You can set the DataGrid to use paging and specify the page size, i.e. the number of records to display per page. However, letting the user to set interactively the page size one can easily run in trouble. Suppose the user is paging a DataGrid with a DataSource of 200 records and the PageSize property set to 5.

    Suppose the user is looking at the last page (page number 40, CurrentPageIndex = 39) seeing the last five records: record number 196 to record number 200. Finally, suppose the user has the ability to change the PageSize to 10. Using only the build-in paging functionality, the DataGrid server control tries to re-display the current page, i.e. page number 40 or CurrentPageIndex = 39, but now there are only 20 pages with 10 records per page. So we end up with a nice error message: "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount".

    Let us find a solution that allows the user to set interactively the DataGird's PageSize property and meanwhile prevent the ugly break up of the application.

    What Happens When Changing the PageSize Property?

    I will start with a sample application taken from Microsoft's ASP.NET QuickStart Tutorial you can find at http://samples.gotdotnet.com/quickstart/aspplus/samples
    /webforms/ctrlref/webctrl/datagrid/VB/datagrid10.aspx
    . The source code can be found at http://samples.gotdotnet.com/quickstart/util/srcview.aspx?
    path=/quickstart/aspplus/samples
    /webforms/ctrlref/webctrl/datagrid/datagrid10.src
    . I have un-essentially changed the sample to suit our investigation. I deleted the CheckBox server control and the corresponding If … Then statement in the Page_Load procedure and I set the PageSize property of the DataGrid server control to 5. The PagerStyle-Mode property of the DataGrid control is already set to "NumericPages". Finally, I added a DropDownList server control inside the <form> element

    <asp:DropDownList
             id="ddlPageSize"
             runat="server"
             AutoPostback="True"
             OnSelectedIndexChanged="PageSizeChanged">
             <asp:ListItem text="1" />
             ..................
             <asp:ListItem text="5" selected=true/>
             <asp:ListItem text="12" />
             ..................
           </asp:DropDownList>

    and I added the PageSizeChanged procedure to handle the SelectedIndexChanged event of the DropDownList control. This procedure sets the PageSize property of the DataGrid to the value of the text attribute of the selected item:

      Sub PageSizeChanged(sender As Object, e As EventArgs)  
        MyDataGrid.PageSize=CType(ddlPageSize.SelectedItem.Text, Integer)
        BindGrid
      End Sub

    Note that I have enabled AutoPostBack for the DropDownList control.

    You can try it out. So long you are displaying the first page you can change the page size as you like, the application works fine. If you try to change the page size while looking at another page, you might or you might not succeed. Whether it works or not depends on the page you are looking at and on the new page size you are setting up. After a few tries we come to a conclusion.

    Changing interactively the page size is of no effect on the current value of the CurrentPageIndex property of the DataGrid control. This property remains unaltered. If the new re-arrangement of the data source according to the new value of the page size contains a page having the index equal to the value of CurrentPageIndex, we are lucky.

    Otherwise the application ends with the known error message. Now it is clear that there must be a fine interplay between the PageSize and the CurrentPageIndex properties of the DataGrid control. Indeed, at the beginning there is such one, while the current page index takes values between 0 and PageCount-1 and PageCount depends on the total number of records and the page size.

    Changing the PageSize we implicitly change the PageCount too, hence we change the admissible range for the current page index. If the CurrentPageIndex happens to be outside the new range, we are in trouble.

    More ASP.NET Articles
    More By Csaba Hatvany


     

    ASP.NET ARTICLES

    - How Caching Means More Ca-ching, Part 2
    - How Caching Means More Ca-ching, Part 1
    - Reading a Delimited File Using ASP.Net and V...
    - What is .Net and Where is ASP.NET?
    - An Object Driven Interface with .Net
    - Create Your Own Guestbook In ASP.NET
    - HTTP File Download Without User Interaction ...
    - Dynamically Using Methods in ASP.NET
    - Changing the Page Size Interactively in a Da...
    - XML Serialization in ASP.NET
    - Using Objects in ASP.NET: Part 1/2
    - IE Web Controls in VB.NET
    - Class Frameworks in VB .NET
    - Cryptographic Objects in C#: Part 1
    - Sample Chapter: Pure ASP.Net







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT