using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UsingServerControls
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Data.SqlClient.SqlDataAdapter SqlPaging;
		protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
		protected System.Data.SqlClient.SqlConnection SqlConn;
		protected System.Data.DataSet PagingDS;
		protected System.Web.UI.WebControls.Repeater OrderData;
		private int CurrentPage;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			//Get first page( 20 rows) of paged grid
			if (!Page.IsPostBack)
				{
				SqlPaging.SelectCommand.Parameters["@lastOrderID"].Value = "";
				CurrentPage = 0;
				SqlPaging.Fill(PagingDS);
				this.OrderData.DataSource = PagingDS;
				this.OrderData.DataMember = this.PagingDS.Tables[0].TableName;
				this.OrderData.DataBind();

				// store DataSet object
				this.ViewState["lastid"] = CalculateLastID(PagingDS);
				}
		}

		private string CalculateLastID(DataSet ds)
			{
			DataTable table = ds.Tables[0];
			if (table.Rows.Count == 0)
				return String.Empty;
			DataRow lastRow;
			DataColumn myColumn;
			// calculate the id of the last row of the current view
			lastRow = table.Rows[table.Rows.Count-1];
			myColumn = table.Columns[0];
			return lastRow[myColumn, DataRowVersion.Current].ToString();
			}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.SqlPaging = new System.Data.SqlClient.SqlDataAdapter();
			this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
			this.SqlConn = new System.Data.SqlClient.SqlConnection();
			this.PagingDS = new System.Data.DataSet();
			((System.ComponentModel.ISupportInitialize)(this.PagingDS)).BeginInit();
			this.OrderData.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.OrderData_ItemCommand);
			// 
			// SqlPaging
			// 
			this.SqlPaging.SelectCommand = this.sqlSelectCommand2;
			this.SqlPaging.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																								new System.Data.Common.DataTableMapping("Table", "GetDataInPages", new System.Data.Common.DataColumnMapping[] {
																																																				  new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
																																																				  new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
																																																				  new System.Data.Common.DataColumnMapping("EmployeeID", "EmployeeID"),
																																																				  new System.Data.Common.DataColumnMapping("RequiredDate", "RequiredDate")})});
			// 
			// sqlSelectCommand2
			// 
			this.sqlSelectCommand2.CommandText = "[GetDataInPages]";
			this.sqlSelectCommand2.CommandType = System.Data.CommandType.StoredProcedure;
			this.sqlSelectCommand2.Connection = this.SqlConn;
			this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
			this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@lastOrderID", System.Data.SqlDbType.VarChar, 40));
			// 
			// SqlConn
			// 
			this.SqlConn.ConnectionString = "data source=localhost;initial catalog=Northwind;persist security info=True;workst" +
				"ation id=haoliu-dev;Trusted_Connection=true;packet size=4096";
			// 
			// PagingDS
			// 
			this.PagingDS.DataSetName = "NewDataSet";
			this.PagingDS.Locale = new System.Globalization.CultureInfo("en-US");
			this.Load += new System.EventHandler(this.Page_Load);
			((System.ComponentModel.ISupportInitialize)(this.PagingDS)).EndInit();

		}
		#endregion

		private void OrderData_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			if ("Next" == e.CommandName)
				{
				CurrentPage ++;
				string lastid = ViewState["lastid"].ToString();
				if (String.Empty == lastid)
					return;
			
				// retrieve the new DataSet
				SqlPaging.SelectCommand.Parameters["@lastOrderID"].Value = lastid;
				PagingDS.Clear();	
				SqlPaging.Fill(PagingDS);

				this.OrderData.DataSource = PagingDS;
				this.OrderData.DataMember = this.PagingDS.Tables[0].TableName;
				this.OrderData.DataBind();
		
				// calculate the last row id of the current view
				this.ViewState["lastid"] = CalculateLastID(PagingDS);
				}
		}
	}
}
