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
{
	/// <summary>
	/// _default 的摘要说明。
	/// </summary>
	public class _default : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label label1;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
		protected System.Web.UI.WebControls.Label lbltitle;
		public string APageTitle;
		private void Page_Load(object sender, System.EventArgs e)
		{
			
			if(!IsPostBack)
			{
				
				this.suyuan();
				this.vote();
			}
		}

		private void vote()
		{
			string strConnection,strSQL;
			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			//				strSQL="select vote,votenum,votetype,votetitle from vote order by voteid desc";
			strSQL="select top 1 vote,votenum,votetype,votetitle from vote where iscmd=true order by voteid desc";
			OleDbConnection objConnection=new OleDbConnection (strConnection);
			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			OleDbDataReader objDataReader = null;
			objConnection.Open ();
			objDataReader=objCommand.ExecuteReader();
			objDataReader.Read();
			string strvote=""; 
			strvote+=objDataReader["vote"]; 
			string strtitle="";
			strtitle+=objDataReader["votetitle"];
			string strvotenum=""; 
			strvotenum+=objDataReader["votenum"]; 
			string strvotetype=""; 
			strvotetype+=objDataReader["votetype"]; 
			string [] vote=strvote.Split('|');
			string [] votenum=strvotenum.Split('|');
			RadioButtonList objRadioButtonList=new RadioButtonList();
			CheckBoxList objCheckBoxList=new CheckBoxList();
			int InAll=0;
			if(Convert.ToInt32(strvotetype)==0)
			{
				for(int i=0;i<vote.Length;i++)
				{
					objRadioButtonList.Items.Add(vote[i]);
					InAll=InAll+Convert.ToInt32(votenum[i]);
				}
				objRadioButtonList.ID="radio1";
				PlaceHolder1.Controls.Add (objRadioButtonList);
			}
			else
			{
				for(int i=0;i<vote.Length;i++)
				{
					objCheckBoxList.Items.Add(vote[i]);
					InAll=InAll+Convert.ToInt32(votenum[i]);
				}
				objCheckBoxList.ID="check1";
				PlaceHolder1.Controls.Add (objCheckBoxList);
			}
			objDataReader.Close();
			objConnection.Close();
			label1.Text=Convert.ToString(InAll);
			this.lbltitle.Text=strtitle;
		}

		private void suyuan()
		{
			//取得数据库路径
			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();
				//取得所需操作的记录ID

				//定义SQL语句
				string ACsql="select ID,WebName from Config where ID=1";
				//创建Command对像
				OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
				//通过执行Command实例化DataReader类
				OleDbDataReader ACdr=ACcomm.ExecuteReader();
				if(ACdr.Read())
				{
					APageTitle=ACdr["WebName"].ToString();
					
					ACdr.Close();

				}
				else
				{
					Response.Write("对不起，请先填加网站信息");
				}
			
			}
			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.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button2_Click(object sender, System.EventArgs e)
		{
			string strConnection,strSQL;
			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			//			strSQL="select voteid,vote,votenum,votetype from vote order by voteid desc";
			strSQL="select top 1 voteid,vote,votenum,votetype from vote where iscmd=true order by voteid desc";
			OleDbConnection objConnection=new OleDbConnection (strConnection);
			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			OleDbDataReader objDataReader = null;
			objConnection.Open ();
			objDataReader=objCommand.ExecuteReader();
			objDataReader.Read();
			int voteid=Convert.ToInt32(objDataReader["voteid"]);
			Response.Redirect("voteview.aspx?voteid="+voteid);
//			Response.Write(@"<script language=JavaScript>window.open('voteview.aspx?voteid="+voteid+"','_blank','left=10,top=20,width=710,height=400');</script>");
			objDataReader.Close();
			objCommand.Dispose();
			objConnection.Close();
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			string strConnection,strSQL;
			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			//			strSQL="select voteid,vote,votenum,votetype from vote order by voteid desc";
			strSQL="select top 1 voteid,vote,votenum,votetype from vote where iscmd=true order by voteid desc";
			OleDbConnection objConnection=new OleDbConnection (strConnection);
			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			OleDbDataReader objDataReader = null;
			objConnection.Open ();
			objDataReader=objCommand.ExecuteReader();
			objDataReader.Read();
			int voteid=Convert.ToInt32(objDataReader["voteid"]);
			string strvotenum=""; 
			strvotenum+=objDataReader["votenum"]; 
			string strvote=""; 
			strvote+=objDataReader["vote"]; 
			string [] vote=strvote.Split('|');
			string [] votenum=strvotenum.Split('|');
			string [] postvote=new string [votenum.Length];
			string votenum_1="";
			int strvotetype=Convert.ToInt32 (objDataReader["votetype"]);
			objDataReader.Close();
			objCommand.Dispose();
			objConnection.Close();
			if(strvotetype==1)//如是多选项
			{
				for(int i=0;i<votenum.Length ;i++)//取得多选项值
				{ 
					if(Request.Form ["check1:"+i+""]=="on")
					{
						postvote[i]="1";
					}
					else
					{
						postvote[i]="0";
					}
				}
			}
			for(int j=0; j<vote.Length ;j++)//循环投票项目数
			{
				if(strvotetype==0)//如是单选项
				{
					if(Request.Form["radio1"]==vote[j])
					{
						votenum[j]=Convert.ToString(Convert.ToInt32(votenum[j])+1);
					}
					votenum_1=""+votenum_1+""+votenum[j]+"|";
				}
				else
				{
					if(postvote[j]!="0")
					{
						votenum[j]=Convert.ToString(Convert.ToInt32(votenum[j])+1);
					}
					votenum_1=""+votenum_1+""+votenum[j]+"|";
				}
			}
  
			int votenumlen;
			votenumlen=votenum_1.Length ;
			votenum_1=votenum_1.Substring(0,votenumlen-1);

			string strconn="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			string strsql="update vote set votenum='"+votenum_1+"' where voteid="+voteid+"";
			OleDbConnection myConnection = new OleDbConnection(strconn);
			OleDbCommand myCommand = new OleDbCommand(strsql, myConnection);
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myCommand.Dispose();
			myConnection.Close();
			//Response.Write (strsql);
			Response.Redirect("voteview.aspx?voteid="+voteid);
//			Response.Write(@"<script language=JavaScript>window.open('voteview.aspx?voteid="+voteid+"','_blank');</script>");
		}
	}
}
