using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Diss_cs{
public class getCate : Control,INamingContainer{
	public Boolean FirstLoad{//第一次加载
		get{
			return (bool)ViewState["FirstLoad"];
		}
		set{
			ViewState["FirstLoad"]=value;
		}
	}
	public int NowSelect{//目前选择的类别ID
		get{
			return Convert.ToInt32(ViewState["NowSelect"]);
		}
		set{
			ViewState["NowSelect"]=value;
		}
	}
	public int TotalRec{//总个数
		get{
			return Convert.ToInt32(ViewState["TotalRec"]);
		}
		set{
			ViewState["TotalRec"]=value;
		}
	}
	public DateTime StartDate{//起始日
		get{
			return Convert.ToDateTime(ViewState["StartDate"]);
		}
		set{
			ViewState["StartDate"]=value;
		}
	}
	public DateTime EndDate{//结束日
		get{
			return Convert.ToDateTime(ViewState["EndDate"]);
		}
		set{
			ViewState["EndDate"]=value;
		}
	}
	public string NowCate{//目前选择类别名称
		get{
			return ViewState["NowCate"].ToString();
		}
		set{
			ViewState["NowCate"]=value;
		}
	}
	public string ConnectSQL{//目前连至SQL的字符串
		get{
			return ViewState["ConnectSQL"].ToString();
		}
		set{
			ViewState["ConnectSQL"]=value;
		}
	}
	public string ServerName{//SQL服务器名称
		get{
			return ViewState["ServerName"].ToString();
		}
		set{
			ViewState["ServerName"]=value;
		}
	}
	public string Uid{//登入SQL的ID
		get{
			return ViewState["Uid"].ToString();
		}
		set{
			ViewState["Uid"]=value;
		}
	}
	public string Pass{//登录SQL的密码
		get{
			return ViewState["Pass"].ToString();
		}
		set{
			ViewState["Pass"]=value;
		}
	}
	public string DBName{// 数据库名称
		get{
			return ViewState["DBName"].ToString();
		}
		set{
			ViewState["DBName"]=value;
		}
	}
public event CommandEventHandler Command;//(Object Sender, CommandEventArgs e);
	protected override void CreateChildControls(){
		ConnectSQL ="server=" + this.ServerName + ";uid=" + this.Uid + ";pwd=" + this.Pass + ";database=" + this.DBName;
		SqlConnection k = new SqlConnection(ConnectSQL);
		SqlDataAdapter Comm = new SqlDataAdapter("select * from Cate order by ID",k);
		DataSet DS = new DataSet();
		Comm.Fill(DS,"Cate");
		DataRow[] Frow;
		if(this.NowSelect == 0){
			this.NowSelect = 1;
		}
		Frow=DS.Tables["Cate"].Select("id =" + this.NowSelect);
		this.NowCate = Frow[0][2].ToString();//取得类别名称
		SqlDataAdapter Comm1 = new SqlDataAdapter("select * from Article",k);
		Comm1.Fill(DS,"Article");//取得文章总数
		this.TotalRec=DS.Tables["Article"].Rows.Count;
		for(int i=0;i<=DS.Tables["Cate"].Rows.Count-1;i++){
			LinkButton lk = new LinkButton();
			lk.ID="lk" + DS.Tables["Cate"].Rows[i]["ID"];
			lk.Text=DS.Tables["Cate"].Rows[i]["cate"].ToString();
			lk.Font.Size=10;//kp.Point(10);
			lk.CommandArgument=DS.Tables["Cate"].Rows[i]["id"].ToString() + ";" + DS.Tables["Cate"].Rows[i]["cate"].ToString();
			lk.Command += new CommandEventHandler(this.CateClick);
			this.Controls.Add(lk);
			this.Controls.Add(new LiteralControl(" "));
		}
		if(this.FirstLoad==true){
			this.Controls.Add(new LiteralControl("<p>"));
			this.Controls.Add(new LiteralControl("总个数："));
			this.Controls.Add(new LiteralControl(this.TotalRec.ToString()));
			this.Controls.Add(new LiteralControl("　"));
			this.Controls.Add(new LiteralControl("目前类别："));
			this.Controls.Add(new LiteralControl(this.NowCate));
			this.Controls.Add(new LiteralControl("　"));
			this.Controls.Add(new LiteralControl("目前范围："));
			this.Controls.Add(new LiteralControl(this.StartDate.ToString("yyyy/MM/dd")));
			this.Controls.Add(new LiteralControl("至"));
			this.Controls.Add(new LiteralControl(this.EndDate.ToString("yyyy/MM/dd")));
			this.FirstLoad=false;
		}

	}
        private void CateClick(Object Sender, CommandEventArgs e){
		string h=e.CommandArgument.ToString();
		int x=h.IndexOf(";");
		this.NowSelect=Convert.ToInt32(h.Substring(0,x));
		this.NowCate=h.Substring(x+1,h.Length-x-1);
		this.Controls.Add(new LiteralControl("<p>"));
		this.Controls.Add(new LiteralControl("总个数："));
		this.Controls.Add(new LiteralControl(this.TotalRec.ToString()));
		this.Controls.Add(new LiteralControl("　"));
		this.Controls.Add(new LiteralControl("目前类别："));
		this.Controls.Add(new LiteralControl(this.NowCate.ToString()));
		this.Controls.Add(new LiteralControl("　"));
		this.Controls.Add(new LiteralControl("目前范围："));
		this.Controls.Add(new LiteralControl(this.StartDate.ToString("yyyy/MM/dd")));
		this.Controls.Add(new LiteralControl("至"));
		this.Controls.Add(new LiteralControl(this.EndDate.ToString("yyyy/MM/dd")));
		CommandEventArgs args = new CommandEventArgs(e);
	        OnCommand(args);
	}
        protected void OnCommand(CommandEventArgs e){
		Command(this,e);//引发事件
	}

}
}