3

I am using my custom DataTable as a DataSource to my ListView. Now, the problem i am facing is that the Paging is not working.

What i want to do is that when i click on page 1 , 2 and so on and so forth, i should be able to fetch only 10 rows at a time.

What i mean is that when the page loads i want to fetch only the first 10 rows, on clicking on 2 i want to fetch the next ten rows. But clicking back on page 1 should not execute the SQL statement again, just fetch the data from somewhere(ViewState/Cache).

What is the correct approach? How should i proceed??

NOTE: I do not want to use SQL DataSource

user1593175
  • 327
  • 5
  • 14

1 Answers1

0

What you need is a PagingDataSource...

protected void Page_Load(object sender, EventArgs e)
{
    BindData((hdnPage.Value != "" ? Convert.ToInt32(hdnPage.Value) : 0));
}

private void BindData(int? pageNo)
{
    PagedDataSource ds = new PagedDataSource();
    ds.DataSource = [YOUR DATA SOURCE];
    ds.AllowPaging = true;
    ds.PageSize = 10;
    ds.CurrentPageIndex = pageNo ?? 0;

    pnlPaging.Controls.Clear();
    for (int i = 0; i < ds.PageCount; i++)
    {
        if (ds.PageCount < 2)
        {
            break;
        }

        if (pageNo == i || (pageNo == null && i == 0))
        {
            pnlPaging.Controls.Add(new LiteralControl("<span style=\"display:inline-block;margin:0 2px 2px 0;\">" + (i + 1).ToString() + "</span>"));
            continue;
        }

        SuprLinkButton lb = new SuprLinkButton()
        {
            CommandName = "pageThis",
            CommandArgument = i.ToString(),
            ID = "lbPage" + i.ToString(),
            Text = (i + 1).ToString()
        };
        lb.Attributes.Add("style", "display:inline-block;margin:0 2px 2px 0;");
        lb.Command += new CommandEventHandler(lb_Command);
        pnlPaging.Controls.Add(lb);
    }

    [LISTVIEW].DataSource = ds;
    [LISTVIEW].DataBind();
}

I have a panel to hold all the page links and a hiddenfield to save the current page index. Then there is the paging command...

protected void lb_Command(object sender, CommandEventArgs e)
{
    hdnPage.Value = e.CommandArgument.ToString();
    BindData(Convert.ToInt32(e.CommandArgument));
}
CoderMarkus
  • 1,118
  • 1
  • 10
  • 24
  • 1
    Thanks for the reply..can you give me the complete example..so that it is easier for me to understand, as i am new to ASP.NET..if you have the project...can you host it somewhere?? – user1593175 Aug 15 '12 at 17:54
  • Unfortunately I can't host the project, but I did update my answer a bit. You have a page load where the initial bind happens (call to BindData) and a command for paging. All you need to do is ad a panel and hidden field to the front end (pnlPaging, hdnPage) and replace [YOUR DATA SOURCE] with your datatable and [LISTVIEW] with the id of your list view. – CoderMarkus Aug 15 '12 at 18:02
  • 1
    Okay thanks, one more thing...in the `DataTable` should i fetch all the rows from the table?? i.e. `SELECT * FROM TABLT-NAME` – user1593175 Aug 15 '12 at 18:05
  • Yes. This solution requires all rows and handles the paging within the complete set of data. – CoderMarkus Aug 15 '12 at 18:06
  • 1
    Excuse me if i am wrong..is it not the same thing?? if i am fetching all the rows , then it would take a lot of time for large tables?right? so how is your solution reducing the load time? – user1593175 Aug 15 '12 at 18:09
  • 1
    I am not aware of any method in .NET to page with a subset data. You would have to custom write it in .NET/SQL if you only want to get rows 1-10 then 11-20, etc. I have used the PagingDataSource with thousands of records and the performance is good, although I am using generic collections instead of DataTables as they are known to have performance issues. – CoderMarkus Aug 15 '12 at 18:31