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 DepsGrid : System.Web.UI.Page
	
    { 



//部门管理窗口自定义控件的声名
		protected CCUtility Utility;
		
		//数据表格表单、变量等的声名
		protected System.Web.UI.HtmlControls.HtmlTableRow deps_no_records;
		protected string deps_sSQL;
		protected string deps_sCountSQL;
		protected int deps_CountPage;
		protected CCPager deps_Pager;protected System.Web.UI.WebControls.LinkButton deps_insert;
		protected System.Web.UI.WebControls.Repeater deps_Repeater;
		protected int i_deps_curpage=1;	
	
		//定义各表单事件保护字符串
		protected string deps_FormAction="DepsRecord.aspx?";
		

//初始化事件对象
	public DepsGrid()
	{
	this.Init += new System.EventHandler(Page_Init);
	}
	
//AdminMenu中的自定义包含控件结束
	public void ValidateNumeric(object source, ServerValidateEventArgs args) {
			try{
				Decimal temp=Decimal.Parse(args.Value);
				args.IsValid=true;
		        }catch{
				args.IsValid=false;	}
		}
//定义登录窗口显示控件过程
//初始化页面过程，创建一个Utility实例，并调用其相应的各方法
        protected void Page_Load(object sender, EventArgs e)
        {	
		Utility=new CCUtility(this);
		Utility.CheckSecurity(3);
		// 完成窗口安全验证
		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);
deps_insert.Click += new System.EventHandler (this.deps_insert_Click);
		deps_Pager.NavigateCompleted+=new NavigateCompletedHandler(this.deps_pager_navigate_completed);
		
        }

//定义整体显示页面过程
        protected void Page_Show(object sender, EventArgs e)
        {
		deps_Bind();
        }

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

//定义每一页显示的部门记录数
const int deps_PAGENUM = 20;

//查询数据库中部门记录数据集合
ICollection deps_CreateDataSource() {

	deps_sSQL = "";
	deps_sCountSQL = "";
	string sWhere = "", sOrder = "";
	
	bool HasParam = false;

	//记录显示的排序方式
	sOrder = " order by d.name Asc";
	if(Utility.GetParam("Formdeps_Sorting").Length>0&&!IsPostBack)
	{ViewState["SortColumn"]=Utility.GetParam("Formdeps_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();
	
	deps_sSQL = "select [d].[dep_id] as d_dep_id, " +
    "[d].[name] as d_name " +
    " from [deps] d ";
	
	//组合Sql语句
	  deps_sSQL = deps_sSQL + sWhere + sOrder;
	  if (deps_sCountSQL.Length== 0) {
	    int iTmpI = deps_sSQL.ToLower().IndexOf("select ");
	    int iTmpJ = deps_sSQL.ToLower().LastIndexOf(" from ")-1;
	    deps_sCountSQL = deps_sSQL.Replace(deps_sSQL.Substring(iTmpI + 7, iTmpJ-6), " count(*) ");
	    iTmpI = deps_sCountSQL.ToLower().IndexOf(" order by");
	    if (iTmpI > 1) deps_sCountSQL = deps_sCountSQL.Substring(0, iTmpI);
	  }
	  
	//数据联结
	
	OleDbDataAdapter command = new OleDbDataAdapter(deps_sSQL, Utility.Connection);
	DataSet ds = new DataSet();
	
	command.Fill(ds, (i_deps_curpage - 1) * deps_PAGENUM, deps_PAGENUM,"deps");
	OleDbCommand ccommand = new OleDbCommand(deps_sCountSQL, Utility.Connection);
	int PageTemp=(int)ccommand.ExecuteScalar();
	deps_Pager.MaxPage=(PageTemp%deps_PAGENUM)>0?(int)(PageTemp/deps_PAGENUM)+1:(int)(PageTemp/deps_PAGENUM);
	bool AllowScroller=deps_Pager.MaxPage==1?false:true;
	
	DataView Source;
        Source = new DataView(ds.Tables[0]);

		if (ds.Tables[0].Rows.Count == 0){
			deps_no_records.Visible = true;
			AllowScroller=false;}
		else
			{deps_no_records.Visible = false;
			AllowScroller=AllowScroller&&true;}
		
		deps_Pager.Visible=AllowScroller;
		return Source;
		//显示完成		
	}
	
//定义导航页
	protected void deps_pager_navigate_completed(Object Src, int CurrPage)
		{
			i_deps_curpage=CurrPage;
				deps_Bind();
		}

//绑定数据
	void deps_Bind() {
		deps_Repeater.DataSource = deps_CreateDataSource();
		deps_Repeater.DataBind();
		
	}

//插入按钮事件
	void deps_insert_Click(Object Src, EventArgs E) {
		string sURL = deps_FormAction+"";
		Response.Redirect(sURL);
	}
//正反排序事件
	protected void deps_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";
		}
		deps_Bind();
	}



    }
}