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>
	/// setupvote 的摘要说明。
	/// </summary>
	public class setupvote : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txttitle;
		protected System.Web.UI.WebControls.RadioButtonList votetype;
		protected System.Web.UI.WebControls.TextBox vote;
		protected System.Web.UI.WebControls.RequiredFieldValidator IsnullTextbox;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Button Button2;
	    public string vote_1;
		protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
		public string votenum;
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(Session["AdminName"]==null)
			{
				Response.Redirect("admin/Admin_Login.aspx");
				
			}
			if(!this.IsPostBack)
			{
				this.bindto();
			}
		}

		#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.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void bindto()
		{
			//			string content;
			//			content=content.Replace("|","/n");
//			int ID=Convert.ToInt32(Request.QueryString["voteid"].Trim());
			string strConnection,strSQL;
			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			strSQL="select iscmd from vote";
			OleDbConnection objConnection=new OleDbConnection (strConnection);
			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			OleDbDataReader objDataReader = null;
			objConnection.Open ();
			
			objDataReader=objCommand.ExecuteReader();
			
			while(objDataReader.Read())
			{

				
				if((bool)objDataReader["iscmd"])
				{
					RadioButtonList1.Items[0].Selected=true;
				}
				else
				{
					RadioButtonList1.Items[1].Selected=true;
				}
			}
			
						objDataReader.Close();
						objConnection.Close();
		}






		private void Button1_Click(object sender, System.EventArgs e)
		{
			string vote_num,votetype,votename,votetitle;
			vote_num="10";
			votetype=Request.Form["votetype"];
			votetitle=Request.Form["txttitle"];
			votename=Request.Form["vote"];
			votename=votename.Replace("'","");
			int i;
			int k;
			string [] vote = System.Text.RegularExpressions.Regex.Split (votename,"\r\n");
			int j=0;
			for( i = 0; i< vote.Length;i++)
			{

				if (!(vote[i]=="" || vote[i]==" "))
				{
					vote_1=""+vote_1+""+vote[i]+"|";
					j=j+1;
				}
				if (i>Convert.ToInt32(vote_num)-1)
				{
					break;
				}

			}
			for( k=0;k<j;k++)
			{
				votenum=""+votenum+"0|";
			}
			int votelen=vote_1.Length;
			int votenumlen=votenum.Length;
			votenum=votenum.Substring(0,votenumlen-1);
			votename=vote_1.Substring(0,votelen-1);
			string strconn="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			string strsql="insert into vote(vote,votenum,votetype,votetitle,iscmd) values('"+votename+"','"+votenum+"',"+Convert.ToInt32(votetype)+",'"+votetitle+"',"+this.RadioButtonList1.SelectedItem.Value+")";
			OleDbConnection myConnection = new OleDbConnection(strconn);
			OleDbCommand myCommand = new OleDbCommand(strsql, myConnection);
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myCommand.Dispose();
			myConnection.Close();
			Label1.Visible=true;
			Button2.Visible =true;
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("vote.aspx");
		}

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