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;
using System.Text;
using System.IO;
namespace baoge
{
	/// <summary>
	/// votelist 的摘要说明。
	/// </summary>
	public class votelist : System.Web.UI.Page
	{
		protected Wuqi.Webdiyer.AspNetPager haha;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
//			if(Session["flag"]==null||Session["flag"].ToString()=="false")
//			{
//				Response.Redirect("login.aspx");
//			}
			if(Session["AdminName"]==null)
			{
				Response.Redirect("admin/Admin_Login.aspx");
				
			}
            
			if(!Page.IsPostBack)
			{
				OleDbConnection objConnection=new OleDbConnection ("provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";");
				OleDbCommand objCommand=new OleDbCommand("select count(*) from vote",objConnection);
				objConnection.Open();
				haha.RecordCount=(int)objCommand.ExecuteScalar();
				objConnection.Close();
				this.BindToDataGrid();
			}
		}

		private void BindToDataGrid()
		{
			
			//			string strConnection,strSQL;
			OleDbConnection objConnection=new OleDbConnection ("provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";");
			//			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			//			strSQL="select * from vote";
			OleDbDataAdapter oda=new OleDbDataAdapter();
			oda.SelectCommand=new OleDbCommand("select * from vote order by voteid desc",objConnection);
			//			OleDbConnection objConnection=new OleDbConnection (strConnection);
			//			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			DataSet ds=new DataSet();
			oda.Fill(ds,haha.PageSize*(haha.CurrentPageIndex-1),haha.PageSize,"vote");
			this.DataGrid1.DataKeyField="voteid";
			this.DataGrid1.DataSource=ds.Tables["vote"];
			this.DataGrid1.DataBind();
			haha.CustomInfoText=" 共有留言<font color=\"blue\"><b>"+haha.RecordCount.ToString()+"</b></font>条";
			haha.CustomInfoText+=" 共有<font color=\"blue\"><b>"+haha.PageCount.ToString()+"</b></font>页";
			haha.CustomInfoText+=" 当前为第<font color=\"red\"><b>"+haha.CurrentPageIndex.ToString()+"</b></font>页";
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
			this.haha.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.haha_PageChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
			{
				e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
				e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
				((LinkButton)(e.Item.Cells[2].Controls[0])).Attributes.Add("onclick","return confirm('确认删除吗?');");
			}
		}

		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			string voteid=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
			int voteidd=int.Parse(voteid);
			//			int   i   =   int.Parse(str); 

			string strConnection,strSQL;
			strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("vote.Mdb")+";";
			strSQL="delete from vote where voteid="+voteidd+"";
			OleDbConnection objConnection=new OleDbConnection (strConnection);
			objConnection.Open();
			OleDbCommand objCommand = new OleDbCommand (strSQL,objConnection);
			objCommand.ExecuteNonQuery();
			this.BindToDataGrid();
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
			this.BindToDataGrid();
		}

		private void haha_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
		{
			haha.CurrentPageIndex=e.NewPageIndex;
			this.BindToDataGrid();
		}
	}
}
