namespace Employee_Directory
{
	
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

	public class Default : System.Web.UI.Page
	
    { 



//定义默认控件声名
		protected CCUtility Utility;
		
		//表格表单、相关控制变量声名
		protected System.Web.UI.HtmlControls.HtmlTableRow Grid_no_records;
		protected string Grid_sSQL;
		protected string Grid_sCountSQL;
		protected int Grid_CountPage;
		
		protected System.Web.UI.WebControls.Repeater Grid_Repeater;
		protected int i_Grid_curpage=1;	
	
		//查询表单、相关控制变量声名
		protected System.Web.UI.WebControls.Button Search_search_button;
		protected System.Web.UI.WebControls.DropDownList Search_dep_id;
		protected System.Web.UI.WebControls.TextBox Search_name;
		protected System.Web.UI.WebControls.TextBox Search_email;
		
		//每月之星员工记录表单、相关变量声名
		protected System.Web.UI.HtmlControls.HtmlTableRow EmpMonth_no_records;
		protected string EmpMonth_sSQL;
		protected string EmpMonth_sCountSQL;
		protected int EmpMonth_CountPage;
		
		protected System.Web.UI.WebControls.Repeater EmpMonth_Repeater;
		protected int i_EmpMonth_curpage=1;	
	
		//每个相应表单事件保护字符串
		protected string Grid_FormAction=".aspx?";
		protected string Search_FormAction="Default.aspx?";
		protected string EmpMonth_FormAction=".aspx?";
		

	
	public Default()
	{
	this.Init += new System.EventHandler(Page_Init);
	}
	
//默认自定义控件声名结束

	public void ValidateNumeric(object source, ServerValidateEventArgs args) {
			try{
				Decimal temp=Decimal.Parse(args.Value);
				args.IsValid=true;
		        }catch{
				args.IsValid=false;	}
		}

// Default Show begin
        protected void Page_Load(object sender, EventArgs e)
        {	
		Utility=new CCUtility(this);
//如果初次装载页面，执行Page_Show
		if (!IsPostBack){
			Page_Show(sender, e);
		}
	}

//关闭页面过程
	protected void Page_Unload(object sender, EventArgs e)
	{
		if(Utility!=null) Utility.DBClose();
	}

//窗口中控件定义事件处理过程
	protected void Page_Init(object sender, EventArgs e)
	{
		InitializeComponent();
	}
        private void InitializeComponent()
        {
		this.Load += new System.EventHandler(this.Page_Load);
		this.Unload += new System.EventHandler(this.Page_Unload);
		Search_search_button.Click += new System.EventHandler (this.Search_search_Click);
        }

//页面中的数据显示过程
        protected void Page_Show(object sender, EventArgs e)
        {
		Grid_Bind();
		Search_Show();
		EmpMonth_Bind();
		
        }

// Default Show end
//完成表单初始化


//定义每页显示记录常数
const int Grid_PAGENUM = 20;

public void Grid_Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e){
	
// Grid Show Event begin
if (e.Item.ItemType==ListItemType.Item  || e.Item.ItemType==ListItemType.AlternatingItem   )
{
 ((Label)e.Item.FindControl("Grid_email")).Text ="<a href=\"mailto:" + ((DataRowView)e.Item.DataItem )["e_email"].ToString() + "\">" + ((DataRowView)e.Item.DataItem )["e_email"].ToString() + "</a>";
}
// Grid Show Event end
}


ICollection Grid_CreateDataSource() {

       
       //开始用于显示数据
	Grid_sSQL = "";
	Grid_sCountSQL = "";

	string sWhere = "", sOrder = "";

	
	bool HasParam = false;
	
	//建立排序方式(逆序或正序)
	sOrder = " order by e.name Asc";
	if(Utility.GetParam("FormGrid_Sorting").Length>0&&!IsPostBack)
	{ViewState["SortColumn"]=Utility.GetParam("FormGrid_Sorting");
	 ViewState["SortDir"]="ASC";}
	if(ViewState["SortColumn"]!=null) sOrder = " ORDER BY " + ViewState["SortColumn"].ToString()+" "+ViewState["SortDir"].ToString();
	
	//建立职工属性
	System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();
	
	
	if(!Params.ContainsKey("dep_id")){
	string temp=Utility.GetParam("dep_id");
	if (Utility.IsNumeric(null,temp) && temp.Length>0) { temp = CCUtility.ToSQL(temp, CCUtility.FIELD_TYPE_Number);} else {temp = "";}
	Params.Add("dep_id",temp);}
	
	if(!Params.ContainsKey("email")){
	string temp=Utility.GetParam("email");
	Params.Add("email",temp);}
	
	if(!Params.ContainsKey("name")){
	string temp=Utility.GetParam("name");
	Params.Add("name",temp);}
	
	  if (Params["dep_id"].Length>0) {
	    HasParam = true;
	    sWhere +="e.[dep_id]=" + Params["dep_id"];
	  }
	  if (Params["email"].Length>0) {
	    if (sWhere.Length >0) sWhere +=" and ";
	    HasParam = true;
	    sWhere +="e.[email] like '%" + Params["email"].Replace( "'", "''") +  "%'";
	  }
	  if (Params["name"].Length>0) {
	    if (sWhere.Length >0) sWhere +=" and ";
	    HasParam = true;
	    sWhere +="e.[name] like '%" + Params["name"].Replace( "'", "''") +  "%'";
	  }

	
	  if(HasParam)
	    sWhere = " AND (" + sWhere + ")";

	//定义Sql语句

	Grid_sSQL = "select [e].[dep_id] as e_dep_id, " +
    "[e].[email] as e_email, " +
    "[e].[emp_id] as e_emp_id, " +
    "[e].[name] as e_name, " +
    "[e].[title] as e_title, " +
    "[e].[work_phone] as e_work_phone, " +
    "[d].[dep_id] as d_dep_id, " +
    "[d].[name] as d_name " +
    " from [emps] e, [deps] d" +
    " where [d].[dep_id]=e.[dep_id]  ";
	
	//装配出完整的Sql语句
	Grid_sSQL = Grid_sSQL + sWhere + sOrder;
	
	OleDbDataAdapter command = new OleDbDataAdapter(Grid_sSQL, Utility.Connection);
	DataSet ds = new DataSet();
	 	
	command.Fill(ds, 0, Grid_PAGENUM, "Grid");
	DataView Source;
        Source = new DataView(ds.Tables[0]);

		if (ds.Tables[0].Rows.Count == 0){
			Grid_no_records.Visible = true;
			}
		else
			{Grid_no_records.Visible = false;
			}
		
		
		return Source;
//表格显示数据完成		
	}
	
//数据绑定过程
	void Grid_Bind() {
		Grid_Repeater.DataSource = Grid_CreateDataSource();
		Grid_Repeater.DataBind();
		
	}
//记录排序方式改变
	protected void Grid_SortChange(Object Src, EventArgs E) {
		if(ViewState["SortColumn"]==null || ViewState["SortColumn"].ToString()!=((LinkButton)Src).CommandArgument){
			ViewState["SortColumn"]=((LinkButton)Src).CommandArgument; 
			ViewState["SortDir"]="ASC";
		}else{
			ViewState["SortDir"]=ViewState["SortDir"].ToString()=="ASC"?"DESC":"ASC";
		}
		Grid_Bind();
	}


//查询显示时获取数据过程
void Search_Show() {
	

	Utility.buildListBox(Search_dep_id.Items,"select dep_id,name from deps order by 2","dep_id","name","All","");
	string s;
	
	s=Utility.GetParam("dep_id");
	
	try {Search_dep_id.SelectedIndex=Search_dep_id.Items.IndexOf(Search_dep_id.Items.FindByValue(s));
	}catch{}
	
	s=Utility.GetParam("name");
	Search_name.Text = s;
	
	s=Utility.GetParam("email");
	Search_email.Text = s;
}

//查询提交过程
void Search_search_Click(Object Src, EventArgs E) {
	string sURL = Search_FormAction + "dep_id="+Search_dep_id.SelectedItem.Value+"&"
	 + "name="+Search_name.Text+"&"
	 + "email="+Search_email.Text+"&"
	;
	//定制查询语句
	sURL += "";
	Response.Redirect(sURL);
}



//最多显示记录数
const int EmpMonth_PAGENUM = 20;

public void EmpMonth_Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e){
	
//显示标题人物的图片
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
   ((Label)e.Item.FindControl("EmpMonth_picture")).Text ="<img border=0 src=\"images/emps/" + ((DataRowView)e.Item.DataItem )["e_picture"].ToString() + "\">";

}
}

//查询符合“每月之星”职工的记录集合
ICollection EmpMonth_CreateDataSource() {

       
	EmpMonth_sSQL = "";
	EmpMonth_sCountSQL = "";

	string sWhere = "", sOrder = "";

	bool HasParam = false;
	
	System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();
	
//组合相应的查询Sql语句
	  sWhere = " WHERE manmonth=1";

	EmpMonth_sSQL = "select [e].[name] as e_name, " +
    "[e].[picture] as e_picture " +
    " from [emps] e ";
	
	  EmpMonth_sSQL = EmpMonth_sSQL + sWhere + sOrder;

//联结数据库，实现查询	
	OleDbDataAdapter command = new OleDbDataAdapter(EmpMonth_sSQL, Utility.Connection);
	DataSet ds = new DataSet();
	 	
	command.Fill(ds, 0, EmpMonth_PAGENUM, "EmpMonth");
	DataView Source;
        Source = new DataView(ds.Tables[0]);

		if (ds.Tables[0].Rows.Count == 0){
			EmpMonth_no_records.Visible = true;
			}
		else
			{EmpMonth_no_records.Visible = false;
			}
		
		
		return Source;
		
	}
	
//绑定查询结果
	void EmpMonth_Bind() {
		EmpMonth_Repeater.DataSource = EmpMonth_CreateDataSource();
		EmpMonth_Repeater.DataBind();
		
	}



    }
}