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 guanli.
	/// </summary>
	public class guanli : System.Web.UI.Page
	{
		protected System.Data.SqlClient.SqlConnection myconn;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox t_1;
		protected System.Web.UI.WebControls.DropDownList d_2;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.LinkButton LinkButton1;
		protected System.Web.UI.WebControls.LinkButton LinkButton2;
		protected System.Web.UI.WebControls.LinkButton LinkButton3;
		protected System.Web.UI.WebControls.LinkButton LinkButton4;
		protected System.Web.UI.WebControls.LinkButton LinkButton5;
		protected System.Data.SqlClient.SqlCommand mycomm;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			myconn=new SqlConnection(Session["str_connection"].ToString());
			myconn.Open();
			try
			{
				if(Request.Cookies["free_huiyuan"].Value.ToString()!="free911")
				{
					try
					{
						Session["ancestor_id"]=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);
					}
					catch
					{
						Response.Redirect("guanlidenglu.aspx?ancestor_id="+Request.Params["ancestor_id"]+"");
					}
					SqlDataReader myread;
					myread=mycomm.ExecuteReader();
					try
					{
						myread.Read();
						Session["leibie"]=myread["neirong"].ToString().Trim();
						myread.Close();
				
					}
					catch
					{
						myread.Close();
						Response.Redirect("guanlidenglu.aspx?ancestor_id="+Request.Params["ancestor_id"]+"");
					}
				}
			}
			catch
			{
				Response.Redirect("guanlidenglu.aspx?ancestor_id="+Request.Params["ancestor_id"]+"");
			}
			
			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.LinkButton5.Click += new System.EventHandler(this.LinkButton5_Click);
			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.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion


		private void BindGrid()
		{
			string sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
			switch (d_2.SelectedIndex)
			{
				case 0:
					if(t_1.Text.Trim()=="")
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					else
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts  where shijian='"+t_1.Text.Trim().ToString()+"' and ancestor_id="+Request.Params["ancestor_id"]+" ";
					break;
				case 1:
					if(t_1.Text.Trim()=="")
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					else
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts  where shijian>='"+t_1.Text.Trim().ToString()+"'and ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					break;
				case 2:
					if(t_1.Text.Trim()=="")
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					else
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts  where shijian<='"+t_1.Text.Trim().ToString()+"' and ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					break;
					
				case 3:
					if(t_1.Text.Trim()=="")
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					else
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts  where shijian>'"+t_1.Text.Trim().ToString()+"' and ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					break;
				case 4:
					if(t_1.Text.Trim()=="")
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts where ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc";
					else
						sqlstr="select postid, zhuti,attachfile,shijian,attachfileext,lujing from posts  where shijian<'"+t_1.Text.Trim().ToString()+"' and ancestor_id="+Request.Params["ancestor_id"]+" order by shijian desc ";
					break;
			}
			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();
		}

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

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

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

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

		private void LinkButton5_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("guanli2.aspx?ancestor_id="+Request.Params["ancestor_id"]+"");
		
			
		}
	}
}
