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_AddChannel 的摘要说明。
	/// </summary>
	public class Admin_AddChannel : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox ChannelName;
		protected System.Web.UI.WebControls.TextBox ReadMe;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.DropDownList OpenType;
		protected System.Web.UI.WebControls.DropDownList ModuleType;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList2;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList3;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList4;
		protected System.Web.UI.WebControls.Button Button1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(Session["AdminName"]==null)
			{
				Response.Redirect("Admin_Login.aspx");
			}
		}

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

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			//取得数据库路径
			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 ACsql1="select ChannelID,ispic from Channel where ChannelName='"+ChannelName.Text+"'";
				OleDbCommand ACcomm1=new OleDbCommand(ACsql1,ACconn);
				OleDbDataReader ACdr1=ACcomm1.ExecuteReader();
				if(ACdr1.Read())
				{
					if((bool)ACdr1["ispic"])
					{
						//						i=(int)ACdrII["Hits"];
						this.RadioButtonList1.Items[0].Selected=true;
					}
					else
					{
						
						this.RadioButtonList1.Items[1].Selected=true;
					}
					if((bool)ACdr1["issoft"])
					{
						//						i=(int)ACdrII["Hits"];
						this.RadioButtonList2.Items[0].Selected=true;
					}
					else
					{
						
						this.RadioButtonList2.Items[1].Selected=true;
					}
					if((bool)ACdr1["isphoto"])
					{
						//						i=(int)ACdrII["Hits"];
						this.RadioButtonList3.Items[0].Selected=true;
					}
					else
					{
						
						this.RadioButtonList3.Items[1].Selected=true;
					}
					if((bool)ACdr1["isphoto"])
					{
						//						i=(int)ACdrII["Hits"];
						this.RadioButtonList4.Items[0].Selected=true;
					}
					else
					{
						
						this.RadioButtonList4.Items[1].Selected=true;
					}
					Response.Write("<script>alert('此频道已存在');</script>");
					Response.Write("<script>history.go(-1);</script>");

				}
				else{
					
					ACdr1.Close();
					//定义所需SQL
					string ACsql="insert into Channel(ChannelName,ReadMe,OpenType,ModuleType,ispic,issoft,isphoto,ismovie) values ('"+ChannelName.Text+"','"+ReadMe.Text+"','"+OpenType.SelectedValue+"','"+ModuleType.SelectedValue+"',"+this.RadioButtonList1.SelectedItem.Value+","+this.RadioButtonList2.SelectedItem.Value+","+this.RadioButtonList3.SelectedItem.Value+","+this.RadioButtonList4.SelectedItem.Value+")";
					//创建Command对像
					OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
					ACcomm.ExecuteNonQuery();
					Response.Write("<script>alert('频道成功添加');</script>");
					
				}

			}
			catch(Exception ex)
			{
			Response.Write(ex.ToString());
			}
			finally
			{
			ACconn.Close();
			}
		}

		private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int ispic;
			if(RadioButtonList1.SelectedItem.Value=="1")
			{
				ispic=1;
			}
			else
			{
				ispic=0;
			}
		}

		private void RadioButtonList2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int issoft;
			if(RadioButtonList2.SelectedItem.Value=="1")
			{
				issoft=1;
			}
			else
			{
				issoft=0;
			}
		}

		private void RadioButtonList3_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int isphoto;
			if(RadioButtonList3.SelectedItem.Value=="1")
			{
				isphoto=1;
			}
			else
			{
				isphoto=0;
			}
		}

		private void RadioButtonList4_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int ismovie;
			if(RadioButtonList4.SelectedItem.Value=="1")
			{
				ismovie=1;
			}
			else
			{
				ismovie=0;
			}
		}
	}
}
