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;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
	/// <summary>
	/// DataGridPagerDelItem 的摘要说明。
	/// </summary>
	public class DataGridPagerDelItem : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid dgPagerDel;
		protected int lastEditedPage;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//页面初试化时进行数据绑定
			if(!IsPostBack)
				DataGridDataBind();
		}

		private void DataGridDataBind()
		{
			//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
			//创建数据适配器对象
			SqlDataAdapter da = new SqlDataAdapter("select EmployeeID,LastName,FirstName,City from Employees",conn);
			//创建DataSet对象
			DataSet ds = new DataSet();
			try
			{
				//填充数据集
				da.Fill(ds,"testTable");
				//进行数据绑定
				dgPagerDel.DataSource = ds.Tables["testTable"];
				dgPagerDel.DataBind();
			}
			catch(Exception error)
			{
				//输出异常信息
				Response.Write(error.ToString());
			}		
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgPagerDel.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPagerDel_PageIndexChanged);
			this.dgPagerDel.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPagerDel_DeleteCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dgPagerDel_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			//设置当前页的索引
			dgPagerDel.CurrentPageIndex = e.NewPageIndex;
			//重新进行数据绑定
			DataGridDataBind();
		}

		private void dgPagerDel_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int empID = (int)dgPagerDel.DataKeys[e.Item.ItemIndex];
			string sqlCom = "delete Employees where EmployeeID="+empID.ToString();
			//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
			//定义命令对象
			SqlCommand cmd = new SqlCommand(sqlCom,conn);
			//打开数据连接
			conn.Open();
			try
			{
				//执行SQL命令
				cmd.ExecuteNonQuery();
				//取得当前页的索引
				lastEditedPage = dgPagerDel.CurrentPageIndex;
				//如果有多页并且当前页中的项数仅有一项
				if ((dgPagerDel.PageCount - dgPagerDel.CurrentPageIndex) == 1 && dgPagerDel.Items.Count == 1)
				{
					if (dgPagerDel.PageCount > 1)
					{
						lastEditedPage = lastEditedPage - 1;
					}
					else
					{
						lastEditedPage = 0;
					}
				}
				dgPagerDel.CurrentPageIndex = lastEditedPage;
				DataGridDataBind();
			}
			catch(SqlException err)
			{
				//输出异常信息
				Response.Write(err.ToString());
			}
			finally
			{
				//关闭连接对象
				conn.Close();
			}
		}
	}
}
