tags:

views:

411

answers:

2

How to do Paging in Datalist. I need Page numbers as follows:

<1 2 3 4 5> When Clicking '>' next 5 pages should display...

Let me Clear my coding flow... I will get the lists from the database using stored procedure with entire records. Then, according to the number of pages, the page number should be displayed. I need only limited number of page numbers to be shown in a page. When, I click next button, next set of page number should be displayed. My stored procedure will return dataset. This is my code for Paging..

private void doPaging() {

    DataTable dt = new DataTable();
    dt.Columns.Add("PageIndex");
    dt.Columns.Add("PageText");
    for (int i = 0; i <= totalpage; i++)
    {
        DataRow dr = dt.NewRow();
        dr[0] = i;
        dr[1] = i + 1;
        dt.Rows.Add(dr);
    }

    dlPaging.DataSource = dt;
    dlPaging.DataBind();
}

This code will display all page numbers once in a page.. Instead I need only 5 page numbers should display once. When I click next button, next 5 page numbers should be displayed.

+4  A: 

Since you did not specify the exact language and framework you are using. I will try to give you a general answer on how to successfully create a paging.

I assume that you have a website or if it would be a Windows Application that you have a Next and Previous button.

You need to have the following in your application:

  • Current index
  • List Size

Now if you want to have 20 items per page you divide the number by 20 and if there is a rest of that division, you know that there will be an extra page.

Example

ListSize = 10

AmountOfPages = ListSize / 20

if ( (ListSize % 20 ) != 0 )
    AmountOfPages += 1;

Now when we know how many pages to print out, you can decied to print them all or not, that is of course your choice.

The next step would be to have a next and previous button handeling the increment and decrement on the current index.

Whenever you press Previous, Next or a Number in your List Pager, you can easily calculate the current start index and the end index.

Since you know that your total amount of items on your current page can be 20 and the smallest index is 0. If you go by that rule you can always do:

index * 20 and index * 20 + 20 to get the current start index of your list and the current end index of your list.

Example

CurrentIndex = 0

ListStart = CurrentIndex * 20
ListEnd = CurrentIndex * 20 + 20

This will result in:

ListStart = 0
ListEnd = 20

And if you were on Page Index 1 you will get the following

ListStart = 20
ListEnd = 40

Now all you have to do is set the Limit on how many items and where to start getting the items.

This of course depends on the data source or data template used for your list. You should look into the API of the Template or Data Source that you are using.

But the general idea if you look at a basic Pseudo statement here:

select Columns from Source limit From, To

You want to tell your datasource or sql query to just Limit by the given Start and End Index. The example above applies more to SQL than other Data Sources. But it should give you an idea on how to solve the problem.


Edit

As I understand you re-post you want to get 1 - 5 when you are on the first page, those are the page indexes of course. And if you press Next you want 6 - 10.

If you think about it, it's not that hard if you follow the above logic, you have the Page Index and all you want to do is add 5 to that each time you press "Next" and reduce it by 5 when you press Previous.

This would result in somewhat the following

Example

doPaging(int direction)
{
    // Direction represents the previous and next to make it easier

    if ( direction == 0 ) // previous
      CurrentIndex -= 5
    else if ( direction == 1 ) // next
      CurrentIndex += 5

    if ( CurrentIndex > MaxIndex || CurrentIndex < 0 )
        CurrentIndex = 0
}

Assumed that you have globally defined Max Index, from the previous example that should be easy.

Now if you have

CurrentIndex = 0

And call doPaging(1) the result of CurrentIndex will be CurrentIndex = 5. Now you can use this to repeat the current indexes available to navigate to.

Regarding SQL Server and limiting rows you can look into this

Filip Ekberg
I can't get u. dlpaging is an another Datalist which will display page number alone. I already assigned the totalpage to it.. I want to reduce it to 5. How to do this?
Nila
I can't really understand what you mean but my post should give you as much information as you need to adapt the software you are writing to acheive the paging you want.
Filip Ekberg
How could I use my dataset to fill only 10 list when that page number is clicked? My dataset has already filled with entire records from the stored procedure
Nila
Is it stored in memory when you do a postBack and no post back?
Filip Ekberg
No postback....
Nila
Sorry.... Y Negative Marks?
Nila
Is it stored in memory then? in your datatable? well then there is a "set row count"-property you can use. Look at the API as i stated in my respons.
Filip Ekberg
K... I got it..... Why Negative Marks for me?
Nila
You were probably downvoted because people found the question hard to understand and you did re-post the question as an answer.
Filip Ekberg
K.. Thank U. I solved the problem.
Nila
If the answer was sufficient please accept it as correct and answered.
Filip Ekberg
Such a big reply Filip! Appreciable efforts made. :)
Ismail
A: 

When deleted the pages how to goes perivious pages/

anup