Monday, November 2, 2009

Repeater control with paging

public partial class _Default : System.Web.UI.Page
{
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand +=
           new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    private void LoadData()
   {
      SqlConnection cn = new SqlConnection("Data Source=AJTECH4;Initial Catalog=vamsi;Integrated Security=True");
      cn.Open();
      SqlDataAdapter da = new SqlDataAdapter("select * from repeatertask", cn);
      DataTable dt = new DataTable();
      da.Fill(dt);
      cn.Close();
      PagedDataSource pgitems = new PagedDataSource();
      DataView dv = new DataView(dt);
      pgitems.DataSource = dv;
      pgitems.AllowPaging = true;
      pgitems.PageSize = 3;
      pgitems.CurrentPageIndex = PageNumber;
      if (pgitems.PageCount > 1)
      {
         rptPages.Visible = true;
         ArrayList pages = new ArrayList();
         for (int i = 0; i < pgitems.PageCount; i++)
         pages.Add((i + 1).ToString());
         rptPages.DataSource = pages;
         rptPages.DataBind();
      }
      else
         rptPages.Visible = false;
      rptItems.DataSource = pgitems;
      rptItems.DataBind();
   }
   void rptPages_ItemCommand(object source,
                             RepeaterCommandEventArgs e)
   {
      PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
      LoadData();
   }
}
 

No comments:

Post a Comment