using System;
using System.Data;
using System.Data.SqlClient ;

using System.Configuration;
using System.Collections;
using System.Collections.Generic ;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataListWithPager : System.Web.UI.Page
{
    PagedDataSource pds;

    protected void Page_Load(object sender, EventArgs e)
    {
        pds= new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 3;
        pds.DataSource = GetList();

        if (!IsPostBack)
        {
            MessageTotal.Text = GetPageCount().ToString();
            PageTotal.Text = pds.PageCount.ToString();
            PageCurrent.Text = (pds.CurrentPageIndex + 1).ToString();

            Binding();

            for (int i = 1; i <= pds.PageCount; i++)
                DropDownList1.Items.Add(i.ToString());
        }

    }

    private List<Categories> GetList()
    {
        List<Categories> list = new List<Categories>();
        
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);

        string mySql = "select * from Categories  ";
        SqlCommand myCommand = new SqlCommand(mySql, myConnection);

        myConnection.Open();
        SqlDataReader myReader = null;

        myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
            Categories temp = new Categories((int)myReader["CategoryID"],
                                              (string)myReader["CategoryName"],
                                              (string)myReader["Description"]);
                                        
            list.Add(temp); 
        }

        myReader.Close();
        myConnection.Close();

        return list;    
    }

    private  int GetPageCount()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);

        string mySql = "select count(*) from Categories ";
        SqlCommand myCommand = new SqlCommand(mySql, myConnection);

        myConnection.Open();

        int tempInt = Convert.ToInt32(myCommand.ExecuteScalar());

        return tempInt;

    }


    public void Binding()
    {
        DataList1.DataSource = pds;
        DataList1.DataBind();

        Previous.Enabled = true;
        Next.Enabled = true;

        if (pds.CurrentPageIndex == 0)
            Previous.Enabled = false;

        if (pds.CurrentPageIndex == pds.PageCount - 1)
            Next.Enabled = false;

    }
  
    protected void Previous_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = Convert.ToInt32(PageCurrent.Text) - 2;

        PageCurrent.Text = (pds.CurrentPageIndex + 1).ToString();
        Binding();
    }

    protected void Next_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = Convert.ToInt32(PageCurrent.Text);

        PageCurrent.Text = (pds.CurrentPageIndex + 1).ToString();
        Binding();
    }

    protected void First_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = 0;
        PageCurrent.Text = "1";
        Binding();
    }

    protected void Last_Click(object sender, EventArgs e)
    {
        PageCurrent.Text = PageTotal.Text;
        pds.CurrentPageIndex = pds.PageCount - 1;
        Binding();
    }

    protected void Go_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
        Binding();
    }





}
