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.SqlClient;


namespace free.bbs
{
	/// <summary>
	/// Summary description for guanli1.
	/// </summary>
	public class guanli1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.DropDownList d_2;
		protected System.Web.UI.WebControls.TextBox t_1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Data.SqlClient.SqlConnection myconn;
		protected System.Data.SqlClient.SqlCommand mycomm;
		protected System.Web.UI.WebControls.Literal l_1;
		string sqlstr;
		SqlDataReader myread;
		string ans;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			myconn=new SqlConnection(Session["str_connection"].ToString());
			myconn.Open();
			try
			{
				ans=Request.Params["ancestor_id"];
				mycomm=new SqlCommand("select neirong from ancestor where (ancestor_id="+Request.Params["ancestor_id"]+" and banzhu='"+Request.Cookies["free_huiyuan"].Value.ToString()+"')",myconn);
				myread=mycomm.ExecuteReader();
				try
				{
					myread.Read();
					Session["leibie"]=myread["neirong"].ToString().Trim();
					l_1.Text=myread["neirong"].ToString().Trim();
					myread.Close();
				
				}
				catch
				{
					myread.Close();

				}
			}
			catch
			{
				try
				{
					mycomm=new SqlCommand("select zhuti from posts where postid="+Request.Params["post_id"]+" ",myconn);
				}
				catch
				{
					Response.Redirect("error1.aspx");
					
				}
				
				myread=mycomm.ExecuteReader();
				try
				{
					myread.Read();
					l_1.Text="Ö÷Ìâ£º"+myread["zhuti"].ToString().Trim();
					myread.Close();
				}
				catch
				{}
				
			}
			
			myconn.Close();
			BindGrid();
		}
		private void BindGrid()
		{
			try
			{
				string str=Request.Params["post_id"];
				sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by huifu.shijian ";
				switch (d_2.SelectedIndex)
				{
					case 0:
						if(t_1.Text.Trim()=="")
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by huifu.shijian ";
						else
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id and huifu.shijian='"+t_1.Text.Trim().ToString()+"' ";
						break;
					case 1:
						if(t_1.Text.Trim()=="")
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by shijian ";
						else
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id and huifu.shijian>='"+t_1.Text.Trim().ToString()+"'order by huifu.shijian ";
						break;
					case 2:
						if(t_1.Text.Trim()=="")
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by shijian ";
						else
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id and huifu.shijian<='"+t_1.Text.Trim().ToString()+"'order by huifu.shijian ";
						break;
					
					case 3:
						if(t_1.Text.Trim()=="")
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by shijian ";
						else
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id and huifu.shijian>'"+t_1.Text.Trim().ToString()+"'order by huifu.shijian ";
						break;
					case 4:
						if(t_1.Text.Trim()=="")
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id order by shijian ";
						else
							sqlstr="select huifu.huifu_id as huifu_id,huifu.neirong as neirong ,huifu.yonghuming as yonghuming,huifu.shijian as shijian  from posts ,huifu  where  huifu.post_id="+Request.Params["post_id"]+" and posts.postid=huifu.post_id and huifu.shijian<'"+t_1.Text.Trim().ToString()+"'order by huifu.shijian ";
						break;
				}
				
			}
			catch
			{
			}
			myconn.Open();
			SqlDataAdapter mydra=new SqlDataAdapter(sqlstr,myconn);
			DataSet mydas=new DataSet();
			mydra.Fill(mydas,"zhuti");
			DataGrid1.DataSource=mydas.Tables["zhuti"].DefaultView;
			DataGrid1.DataBind();
			myconn.Close();
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			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.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion


		private void Button1_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

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

		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			Response.Write("nihao");
			string deletecmd="delete from huifu where huifu_id=@huifu_id";
			SqlCommand mycomm=new SqlCommand(deletecmd,myconn);
			mycomm.Parameters.Add(new SqlParameter("@huifu_id",SqlDbType.Int));
			mycomm.Parameters["@huifu_id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
			mycomm.Connection.Open();
			mycomm.ExecuteNonQuery();
			mycomm.Connection.Close();
			BindGrid();
		
		}

	}
}
