Custom Paging Asp.Net

In many occasion when client demand customized paging of his own choice , as the graphics designers provide some circles around numbers in paging or boxes or whatever , means that is strictly not applicable with gridview paging or if we need paging for data repeater then there is very good resource available .
If we are using .net 2.0 then we can go for a class PagedDataSource , and in .net 3.5 it gives us pager control DataPager in which we can do very complex customization like starting number , dashes template , number designs etc etc.
And if DataPager wont work again we can go for paged datasource. Here is the working sample of PagedDatasource you can customized it your own way.

Here i did it with repeater , and it can work perfectly with any data control

CODE BEHIND


 public void Paging(int catid)
    {
        SqlConnection cn = new SqlConnection(add_ConfigSection.ConnectionString);
        string sel = “select * from add_product where categoryID= ” + catid;
        SqlCommand cmd = new SqlCommand(sel, cn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable DT = new DataTable();
        adp.Fill(DT);


        
        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = DT.DefaultView ;
        pageds.AllowPaging = true;
        pageds.PageSize = 8;


        int curpage = 0;


        if (Request.QueryString[“page”] != null)
        {
            curpage = Convert.ToInt32(Request.QueryString[“page”]);
        }
        else
        {
            curpage = 1;
        }


        pageds.CurrentPageIndex = curpage – 1;
        
        if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
            lblCurrpage.Text = “Pages: 1”;
        else if (pageds.DataSourceCount == 0)
            lblCurrpage.Text = “No data to display.”;
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
            lblCurrpage.Text = “Pages: 1“;


        for (int i = 2; i < = pageds.PageCount; i++)
        {
            if (i == curpage)
                lblCurrpage.Text = lblCurrpage.Text + ” , ” + i.ToString();
            else
                lblCurrpage.Text = lblCurrpage.Text + ” , ” + i.ToString() + ““;
        }
        
        rptProduct.DataSource = pageds;
        rptProduct.DataBind();


    }

Dont forget to create asplabel with id lblCurrpage on your webform.


Leave a Reply

Your email address will not be published. Required fields are marked *