using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class News : System.Web.UI.Page
{
    protected CuteEditor.Editor Editor1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_value.Text = "添加";
            if (Session["AdminLevel"] == null || Session["AdminLevel"].ToString() != "1")
            {
                Response.Redirect("logon.aspx");
            }
            else
            {
                string action = Request.QueryString["Action"];
                if (action == "Add")
                {
                    btn_value.Text = "添加";
                }
                else if (action == "Edit")
                {
                    btn_value.Text = "编辑";
                    GetNews();
                }
                else if (action == "LogOut")
                {
                    Session.Abandon();
                    Response.Redirect("Default.aspx");
                }
                classlist();
                newslist();

                //绑定类别下拉列表
                if (Request.QueryString["ClassID"] != null)
                {
                    Bind_ddl(Request.QueryString["ClassID"].ToString());
                }
                else
                { 
                 Bind_ddl("4");
                }
            }
        }
    }
    //为下拉列表添加数据
    public void Bind_ddl(string BigClassID)
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        //创建查询语句
        string sql = "select * from BigClass";
        OleDbDataAdapter da = new OleDbDataAdapter(sql, myconn);
        DataSet ds = new DataSet();
        da.Fill(ds, "BigClass");
        ddl_class.DataSource = ds.Tables["BigClass"].DefaultView;
        ddl_class.DataValueField = "BigClassID";
        ddl_class.DataTextField = "BigClassName";
        ddl_class.SelectedValue = BigClassID;
        ddl_class.DataBind();
        myconn.Close();
    }


    public void GetNews()
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        //创建查询语句
        string sql = "select * from News Where ID=" + id;

        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        OleDbDataReader result = mycmd.ExecuteReader();
        if (result.Read())
        {
            tbx_title.Text = result["Title"].ToString();
            Editor1.Text=result["Content"].ToString();
            tbx_author.Text = result["Author"].ToString();
            lbl_addtime.Visible = true;
            lbl_addtime.Text = result["AddTime"].ToString();

            Bind_ddl(result["BigClassID"].ToString());
        }
        myconn.Close();
    }
    
    public void classlist()
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        //创建查询语句
        string sql = "select * from BigClass";

        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        OleDbDataReader result = mycmd.ExecuteReader();

        rpCalss.DataSource = result;
        rpCalss.DataBind();
        //dl_class.DataSource = result;
        //dl_class.DataBind();
        myconn.Close();

    }
    public void newslist()
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        
        //创建查询语句
        if (Request.QueryString["classid"] == null || Request.QueryString["classid"] == "")
        {
            string sql = "select A.BigClassName,B.Title,B.ID,B.ReadCishu from BigClass A, News B Where A.BigClassID = B.BigClassID";

            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            OleDbDataReader result = mycmd.ExecuteReader();
            gv_news.DataSource = result;
            gv_news.DataBind();
            myconn.Close();
        }
        else
        {
            string sql = "select A.BigClassName,B.Title,B.ID,B.ReadCishu from BigClass A, News B Where A.BigClassID = B.BigClassID and B.BigClassID =" + Convert.ToInt32(Request.QueryString["classid"]);

            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            OleDbDataReader result = mycmd.ExecuteReader();

            gv_news.DataSource = result;
            gv_news.DataBind();
            myconn.Close();
        }


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        string action = Request.QueryString["Action"].ToString();
        string title= tbx_title.Text;
        string author= tbx_author.Text;
        int classid =Convert.ToInt32( ddl_class.SelectedValue);
        string content =Editor1.Text;

        //打开数据库连接
        myconn.Open();
        if (action == "Add")
        {
            string sql = "insert into News (BigClassID,Title,Content,Author,AddTime,ReadCishu) Values(" + classid + ",'" + title + "','";
            sql= sql+ content + "','" +author +"','"+ DateTime.Now+"',1)";

            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            mycmd.ExecuteNonQuery();
            myconn.Close();
            message.Visible = true;
            message.Text = "添加成功";
            newslist();
        }
        else if (action == "Edit")
        {
            int id=Convert.ToInt32(Request.QueryString["id"]);
            string sql = "Update News Set Title=" + "'" +title+ "',Content=" +"'" + content+"',BigClassID =" +classid+ ",Author=" + "'" +author+"'";
            sql = sql + " Where ID=" +id;
            //Response.Write(sql);
            //Response.End();
            //创建数据库命令
            OleDbCommand mycmd = new OleDbCommand(sql, myconn);
            mycmd.ExecuteNonQuery();
            myconn.Close();
            message.Visible = true;
            newslist();
            message.Text = "编辑成功";
        }

    }

    protected void gv_news_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string strConnection = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("DB_51aspx\\Data.mdb");
        //创建数据库连接对象
        OleDbConnection myconn = new OleDbConnection(strConnection);
        myconn.Open();
        string sql = "delete * from News Where ID=" + Convert.ToInt32(gv_news.DataKeys[e.RowIndex].Value);
        //创建数据库命令
        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        mycmd.ExecuteNonQuery();
        myconn.Close();
        message.Visible = true;
        message.Text = "删除成功";
        newslist();
    }
}
