Custom paging in a gridview in asp.net

950 Views Asked by At

I want to make a custom paging for my gridview in asp.net 'cause i have a lot of information in my database. My problem is when i generate linkbuttons. I put them in a panel, but i have more than 7000 buttons for my pages. When i add them into my panel, it displays me all of them on 10 lines in my page. I need to display only 10 and when i press the last of them to display other 10 buttons. My code:

for (int index = 0; index < nrPages; index++)
                {
                    int pageNo = index + 1;
                    LinkButton lnk = new LinkButton();
                    lnk.Click += new EventHandler(PageChange);

                    lnk.ID = "PageLink" + pageNo.ToString();
                    lnk.CommandName = "Page";
                    lnk.Text = " " + pageNo.ToString() + " ";
                    lnk.CommandArgument = index.ToString();

                    PanelPager.Controls.Add(lnk);
                }

public void PageChange(object sender, EventArgs e)
        {
            int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
            object dataSource = GetDataSource(OwnerId, null, pageIndex);
            PushData(dataSource);

        }

Here i use my linkButton

1

There are 1 best solutions below

6
Chetan On
int nrPages = 7000;

public void RenderPageNumbers(int nextPageNo)
{
    int pageSize = 10;
    int totalPages = nrPages / pageSize;
    var startIndex = ((nextPageNo -1) * pageSize ) + 1
    for (int index = startIndex; index < startIndex + pageSize; index++)
    {
        int pageNo = index;
        LinkButton lnk = new LinkButton();
        lnk.Click += new EventHandler(PageChange);

        lnk.ID = "PageLink" + pageNo.ToString();
        lnk.CommandName = "Page";
        lnk.Text = " " + pageNo.ToString() + " ";
        lnk.CommandArgument = index.ToString();

        PanelPager.Controls.Add(lnk);
    }
}