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.SqlClient;
public partial class manage_Manageproduct : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
            Bindnews();
        }
    }
    
    //打开产品数据库
    public DataSet Getnews()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "SELECT Top 20 MynewsID,Title,Createdate,Hit,Leibie FROM Mynews ORDER BY Createdate DESC";
        SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
        DataSet ds = new DataSet();
        try
        {
            myConnection.Open();
            da.Fill(ds, "aa");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return ds;
    }
   //绑定控件
    private void Bindnews()
    {
        DataSet ds = Getnews();
        ProductGridView.DataSource = ds.Tables["aa"].DefaultView;
        ProductGridView.DataBind();
    }
    //翻页
    protected void ProductGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ProductGridView.PageIndex = e.NewPageIndex;
        Bindnews();
    }
   
    //按ID删除产品
    private void Deletenews(int nMynewsID)
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "DELETE FROM Mynews WHERE MynewsID=" + nMynewsID;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
    }
    //修改目录
    private void Updatemulu(int nNewsID,string sLeibie)
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "UPDATE Mynews SET Leibie='" + sLeibie + "' WHERE MynewsID=" + nNewsID;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
    }
    protected void movBtn_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in ProductGridView.Rows)
        {
            CheckBox cbox = (CheckBox)row.FindControl("chkBox");
            if (cbox != null)
            {
                if (cbox.Checked == true)
                {
                    int mynewsid = Int32.Parse(ProductGridView.DataKeys[row.RowIndex].Value.ToString());
                    string leibie=DrpLeibie.SelectedValue.ToString();
                    Updatemulu(mynewsid, leibie);
                }
            }
        }
        Bindnews();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in ProductGridView.Rows)
        {
            CheckBox cbox = (CheckBox)row.FindControl("chkBox");
            if (cbox != null)
            {
                if (cbox.Checked == true)
                {
                    int mynewsid = Int32.Parse(ProductGridView.DataKeys[row.RowIndex].Value.ToString());
                    Deletenews(mynewsid);
                }
            }
        }
        Bindnews();
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= ProductGridView.Rows.Count-1; i++)
        {
            CheckBox cbox = (CheckBox)ProductGridView.Rows[i].FindControl("chkBox");
            if (CheckBox1.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
               
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
