ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
ASP.NET Unleashed
ASP.NET Unleashed

Find Prices
Read Review
Sample Chapter
Sample Chapter
Sample Chapter
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

ASP.NET : “Paged” DropDownList control

Teemu Keiski

I have in the last two years developed many, many applications that handle insane amounts of data. And when thinking about the user, in such cases amounts of data will sometimes reflect to the user experience negatively. One problem I noticed was that all kinds of dropdownlists and comboboxes might contains thousands of rows. And in this article I demonstrate how such user experience can be made better by developing simple customized DropDownList control that has paging functionality.


Key things when developing this kind of control:

  1. Inherit the control from DropDownList
  2. Override DataSource property to utilize PagedDataSource class
  3. Implement CurrentPageIndex property to hold page index
  4. Implement GoToNextPage method
  5. Implement GoToPreviousPage method
  6. Implement GoToFirstPage method
  7. Implement GoToLastPage method
  8. Implement PageSize property to hold page size
  9. Override OnDataBinding method to count correct indexes before control is databound


Usage of the control

PagedDDL works like any other data-bound control in ASP.NET. Paging properties are set as needed and then control is data-bound. When page is changed, index is set either manually or by calling paging methods and then control is rebound.


    <cc1:PagedDDL id=PagedDDL1 runat="server" PageSize="5"></cc1:PagedDDL>

<br>

        <asp:Button ID="btnFirst" Runat=server Text="First" />

        <asp:Button ID="btnPrevious" Runat=server Text="Previous" />

        <asp:Button ID="btnNext" Runat=server Text="Next" />

        <asp:Button ID="btnLast" Runat=server Text="Last" />

   

private void Page_Load(object sender, System.EventArgs e)

           {

                      if(!Page.IsPostBack)

                      {

                                 BindDDL();

                      }

           }

//Test data

private void BindDDL()

           {

                      SortedList sl=new SortedList();

                      for(int i=1;i<51;i++)

                                 sl.Add(i,i);

                      PagedDDL1.DataSource = sl;

                      PagedDDL1.DataValueField ="Value";

                      PagedDDL1.DataTextField ="Key";

                      PagedDDL1.DataBind();

           }

                     

private void btnFirst_Click(object sender, System.EventArgs e)

           {

                      PagedDDL1.GoToFirstPage();

                      BindDDL();

           }

private void btnPrevious_Click(object sender, System.EventArgs e)

           {

                      PagedDDL1.GoToPreviousPage();

                      BindDDL();

           }

private void btnNext_Click(object sender, System.EventArgs e)

           {

                      PagedDDL1.GoToNextPage();

                      BindDDL();

           }

private void btnLast_Click(object sender, System.EventArgs e)

           {

                      PagedDDL1.GoToLastPage();

                      BindDDL();

           }

Conclusion

Developing control that implements paging is pretty simple when PagedDataSource class can be utilized. With same technique paging can be brought into DataList or Repeater controls, either included or as external feature, provided by the application.


Drawback with current implementation of PagedDDL is that because paging buttons are not implemented by the control, it has no way to disable for example button that goes to the first page when we already are at the first page. Although this could be developed simply so that DDL either implements those buttons or gets their ID’s provided as properties and when in control’s lifecycle paging details are known (OnDataBinding), manages visibility of buttons as needed.

[Download code][Online demo][Back to article index]
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/21/2009 6:14:23 PM