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 baoge.Admin
{
	/// <summary>
	/// Admin_AddClass 的摘要说明。
	/// </summary>
	public class Admin_AddClass : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox ClassName;
		protected System.Web.UI.WebControls.TextBox ReadMe;
		protected System.Web.UI.WebControls.DropDownList ChannelID;
		protected System.Web.UI.WebControls.Button Submit;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.Label OpenType;
		protected System.Web.UI.WebControls.Label ModuleType;
		protected System.Web.UI.WebControls.DropDownList ParentID;
	
		public int CID,PID;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(Session["AdminName"]==null)
			{
				Response.Redirect("Admin_Login.aspx");
			}


			if(!IsPostBack)
			{
				//取得数据库路径
				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);
				string ACsql="select ChannelID,ChannelName from Channel";
				OleDbDataAdapter ACda=new OleDbDataAdapter(ACsql,ACconn);
				DataTable ACdt=new DataTable();
				ACda.Fill(ACdt);

				//频道的数据绑定
				ChannelID.DataSource=ACdt;
				ChannelID.DataTextField="ChannelName";
				ChannelID.DataValueField="ChannelID";
				ChannelID.DataBind();

				

				

			}


		}

		private void Submit_Click(object sender, System.EventArgs e)
		{
			int CID=Convert.ToInt32(ChannelID.SelectedValue);
			int PID=Convert.ToInt32(ParentID.SelectedValue);
			//取得数据库路径
			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();

				//判断添加频道是否已存在
				string ACsqlII="select ClassID from Class where ClassName='"+ClassName.Text+"'";
				OleDbCommand ACcommII=new OleDbCommand(ACsqlII,ACconn);
				OleDbDataReader ACdrII=ACcommII.ExecuteReader();
				if(ACdrII.Read())
				{
					Response.Write("<script>alert('此栏目已存在');</script>");
					Response.Write("<script>history.go(-2);</script>");

				}
				else
				{
					
					ACdrII.Close();

					if(OpenType.Text=="原窗口打开")
					{
						OpenType.Text="0";	
					}
					else
					{
						OpenType.Text="1";

					}
					if(ModuleType.Text=="信息")
					{
						ModuleType.Text="1";

					}
					else if(ModuleType.Text=="下载")
					{
						ModuleType.Text="2";

					}
					else
					{
						ModuleType.Text="3";

					}

					string ACsqlI="insert into Class (ChannelID,ClassName,ParentID,Readme,OpenType,ModuleType) values ("+CID+",'"+ClassName.Text+"',"+PID+",'"+ReadMe.Text+"','"+OpenType.Text+"','"+ModuleType.Text+"')";
					OleDbCommand ACcommI=new OleDbCommand(ACsqlI,ACconn);
					ACcommI.ExecuteNonQuery();
					Response.Write("<script>alert('栏目成功添加');</script>");
				}
			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();
			}
		}


		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.ChannelID.SelectedIndexChanged += new System.EventHandler(this.ChannelID_SelectedIndexChanged);
			this.Submit.Click += new System.EventHandler(this.Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		
		private void ChannelID_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int CID=Convert.ToInt32(ChannelID.SelectedValue);
				
			//取得数据库路径
			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);
			
			//打开数据库
			ACconn.Open();
			//定义取得打开方式和频道类型的SQL
			string ACsql1="select OpenType,ModuleType from Channel where ChannelID="+CID;

			OleDbCommand ACcomm1=new OleDbCommand(ACsql1,ACconn);
			OleDbDataReader ACdr1=ACcomm1.ExecuteReader();
			if(ACdr1.Read())
			{
				if(ACdr1["OpenType"].ToString()=="0")
				{
					OpenType.Text="原窗口打开";
				}
				else
				{
					OpenType.Text="新窗口打开";

				}
				if(ACdr1["ModuleType"].ToString()=="1")
				{
					ModuleType.Text="信息";

				}
				else if(ACdr1["ModuleType"].ToString()=="2")
				{
					ModuleType.Text="下载";

				}
				else
				{
					ModuleType.Text="产品";

				}

			}
			else
			{
				OpenType.Text="添加频道时没有选择此项";
				ModuleType.Text="添加频道时没有选择此项";
			}
			ACdr1.Close();
			ACconn.Close();
			ClassDataGrid();
			
		}
		private void ClassDataGrid()
		{
			
			int CID=Convert.ToInt32(ChannelID.SelectedValue);
				
			//取得数据库路径
			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);
			//取得栏目数据
			string ACsql2="select ClassName,ClassID from Class where ChannelID=0";
			OleDbDataAdapter ACda2=new OleDbDataAdapter(ACsql2,ACconn);
			DataTable ACdt2=new DataTable();
			ACda2.Fill(ACdt2);

			//栏目的数据绑定
			ParentID.DataSource=ACdt2;
			ParentID.DataTextField="ClassName";
			ParentID.DataValueField="ClassID";
			ParentID.DataBind();

		}

		
	}
}
