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.OleDb;

namespace ASPX163.NET.Admin
{
	/// <summary>
	/// Admin_ListUser 的摘要说明。
	/// </summary>
	public class Admin_ListUser : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid ListUserGrid;
		protected System.Web.UI.WebControls.Button Button1;
	
		public int StartIndex=0;
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面

			if(Session["AdminName"]==null)
			{
				Response.Redirect("Admin_Login.aspx");
				
			}
			if(!this.IsPostBack)
			{
				ListUserDataBind();
			}
		}

		protected void ListUserDataBind()
		{
		
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			//定义SQL语句
			string ACsql="select * from Admin";
			//定义数据适配器
			OleDbDataAdapter ACda=new OleDbDataAdapter(ACsql,ACconn);
			//创建数据集
			DataSet ACds=new DataSet();
			//从指定的索引开始取PageSize条记录
			ACda.Fill(ACds,StartIndex,ListUserGrid.PageSize,"CurDataTable");
			//创建所有数集
			ACda.Fill(ACds,"AllDataTable");
			//设置DataGrid控件实际显示的项数
			ListUserGrid.VirtualItemCount=ACds.Tables["AllDataTable"].Rows.Count;
			//数据绑定
			this.ListUserGrid.DataKeyField="ID";
			ListUserGrid.DataSource=ACds.Tables["CurDataTable"];
			ListUserGrid.DataBind();

		}

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

		}
		#endregion

		private void ListUserGrid_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}


		private void ListUserGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			//设置DataGrid当前页的索引值为用户选择的页的索引
			ListUserGrid.CurrentPageIndex = e.NewPageIndex;
			//取得当前页为止总共有多少条记录，以便在下一页就从该记录开始读取
			StartIndex = ListUserGrid.PageSize * ListUserGrid.CurrentPageIndex;
			//重新绑定数据
			ListUserDataBind();
		}

		private void ListUserGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
		    string empID=this.ListUserGrid.DataKeys[e.Item.ItemIndex].ToString();
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对象
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			//操作数据库
			try
			{
				//打开数据库
				ACconn.Open();
				//定义SQL语句
				string ACsql="delete from Admin where ID="+empID;
				//定义Command对像
				OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
				//执行SQL
				ACcomm.ExecuteNonQuery();
				this.ListUserDataBind();
			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();
			}
		}

		private void ListUserGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
			{
				e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
				e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
				((LinkButton)(e.Item.Cells[11].Controls[0])).Attributes.Add("onclick","return confirm('确认删除吗?');");
				
			}
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			foreach(System.Web.UI.WebControls.DataGridItem dl in this.ListUserGrid.Items)
			{
				CheckBox chk=(CheckBox)dl.FindControl("CheckBox2");
				if(chk.Checked)
				{
					Response.Write(dl.Cells[1].Text);
				}
			}
		}
	}
}
