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>
	/// remark 的摘要说明。
	/// </summary>
	public class remark : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox plcontent;
		protected System.Web.UI.WebControls.TextBox plname;
		protected System.Web.UI.WebControls.Label title;
		protected System.Web.UI.WebControls.Button submit;
		protected System.Web.UI.WebControls.Button Button5;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Button Button4;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.Button Button3;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.DataList DataList1;
		protected System.Web.UI.WebControls.Label Label1;
	    public int y;
		private void Page_Load(object sender, System.EventArgs e)
		{
			
			if(!this.IsPostBack)
			{
				
				this.Label2.Text="1";
				this.databind();
				this.TextBox1.Text="1";
			   int id=Convert.ToInt32(Request.QueryString["id"].ToString());
			   string title=Request.QueryString["title"].ToString();
			   this.title.Text="<a href=NewsView.aspx?id="+id+">"+title+"</a>";

			}
		}

		

		private void databind()
		{
			
			

//			int CID=Convert.ToInt32(Request.QueryString["id"]);
			int id=Convert.ToInt32(Request.QueryString["id"].ToString());


			


			string title=Request.QueryString["title"].ToString();
			int curPage=Convert.ToInt32(this.Label2.Text);
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义连接字符串
			string ConnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库对像
			OleDbConnection connection=new OleDbConnection(ConnStr);

			String mysql="SELECT * FROM pl where ArticleID="+id+" and Title='"+title+"' order by plid desc";
			//			String mysql="SELECT * FROM Article where ispic=true order by ArticleID desc";
			OleDbDataAdapter sda=new OleDbDataAdapter(mysql,connection);


			
			//			sda.SelectCommand=new SqlCommand("select * from employees",con);
			DataSet ds=new DataSet();
			sda.Fill(ds,"pl");
			System.Web.UI.WebControls.PagedDataSource ps=new PagedDataSource();
			ps.DataSource=ds.Tables["pl"].DefaultView;
			ps.AllowPaging=true;
			ps.PageSize=5;
			ps.CurrentPageIndex=curPage-1;
			this.Button1.Enabled=true;
			this.Button2.Enabled=true;
			
			if(curPage==1)
			{
				this.Button1.Enabled=false;
			}
			if(curPage==ps.PageCount)
			{
				this.Button2.Enabled=false;
			}
			
			
			this.DataList1.DataSource=ps;
			this.DataList1.DataBind();
			this.Label3.Text=Convert.ToString(ps.PageCount);
		}


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

		}
		#endregion

		private void submit_Click(object sender, System.EventArgs e)
		{
			
			
					 

			
			int id=Convert.ToInt32(Request.QueryString["id"].ToString());
			string title=Request.QueryString["title"].ToString();
			//			this.title.Text="<a href='NewsView.aspx?id="+Request.QueryString["id"]+"'>'"+Request.QueryString["title"].ToString()+"'</a>";
			//取得数据库路径
			
			int face=Convert.ToInt32(Request.Form["face"]);
			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 Addtime;
				Addtime =DateTime.Now.ToString();
				
				//定义SQL语句
				
				string ACsql="insert into pl (ArticleID,Title,plname,plcontent,pltime,plface)values ("+id+",'"+title+"','"+this.plname.Text+"','"+this.plcontent.Text+"','"+Addtime+"',"+face+")";

				//创建Command对像
				OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
				//执行sql
				ACcomm.ExecuteNonQuery();
				Response.Write("<script>alert('评论成功添加');</script>");
				this.databind();
			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				//关闭数据库
				ACconn.Close();
			}
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label2.Text)-1);
			this.databind();
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label2.Text)+1);
			this.databind();
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			this.Label2.Text="1";
			this.databind();
		}

		private void Button4_Click(object sender, System.EventArgs e)
		{
			this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label3.Text));
			this.databind();
		}

		private void Button5_Click(object sender, System.EventArgs e)
		{
			if(this.TextBox1.Text.ToString()=="0")
			{
				this.Label2.Text="1";
				this.databind();
			}
			else if(this.TextBox1.Text.ToString()=="")
			{
				this.Label2.Text="1";
				this.databind();
			}
		  
			else
			{
				this.Label2.Text=Convert.ToString(this.TextBox1.Text);
				this.databind();
			}
		}


		
	}
}
