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>
	/// DataGridCheckBox 的摘要说明。
	/// </summary>
	public class DataGridCheckBox : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid dgCheckBox;
		protected System.Web.UI.WebControls.Button cmdSelectAll;
		protected System.Web.UI.WebControls.Button cmdFindSelected;
		protected System.Web.UI.WebControls.Label Message;

		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 top 5 LastName,FirstName,City from Employees",conn);
			//创建DataSet对象
			DataSet ds = new DataSet();
			try
			{
				//填充数据集
				da.Fill(ds,"testTable");
				//进行数据绑定
				dgCheckBox.DataSource = ds.Tables["testTable"];
				dgCheckBox.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.cmdSelectAll.Click += new System.EventHandler(this.cmdSelectAll_Click);
			this.cmdFindSelected.Click += new System.EventHandler(this.cmdFindSelected_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void cmdSelectAll_Click(object sender, System.EventArgs e)
		{
			System.Web.UI.WebControls.CheckBox chkExport;
			//如果要全选
			if(cmdSelectAll.Text == "全选")
			{
				//循环设置DataGrid控件中的项
				foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
				{
					//建立模板列中CheckBox控件的引用
					chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
					//选中
					chkExport.Checked =true;
				}

				cmdSelectAll.Text = "全消";
			}
			else
			{
				foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
				{
					//建立模板列中CheckBox控件的引用
					chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
					//取消
					chkExport.Checked = false;
				}
				cmdSelectAll.Text = "全选";
			}
		}

		private void cmdFindSelected_Click(object sender, System.EventArgs e)
		{
			System.Web.UI.WebControls.CheckBox chkExport;
			String sID;
			System.Text.StringBuilder strMsg = new System.Text.StringBuilder("选中项的City字段值分别为：<hr color=red>");
			//循环取的DataGrid控件中选定项的值
			foreach(DataGridItem oDataGridItem in dgCheckBox.Items)
			{

				chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
				//如果选中了则取值
				if(chkExport.Checked)
				{
					sID = ((Label)(oDataGridItem.FindControl("lblColumn"))).Text;
					strMsg.Append(sID+"<br><hr color=red>");
				}
			}
			//显示选中项的值
			Message.Text = strMsg.ToString();
		}

	}
}
