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;
using System.IO;

public partial class include_Inputlei : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        BindLeiGridView();
    }
    //绑定控件
    private void BindLeiGridView()
    {
        DataSet ds = GetLeibie();
        LeiGridView.DataSource = ds.Tables["aa"].DefaultView;
        LeiGridView.DataBind();
    }
    //读取类别
    public DataSet GetLeibie()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "SELECT * FROM Product WHERE FOLDER=1 ORDER BY Showorder 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 UpdateLei(string sName, int nOrder, int nLeiID)
    {
        string myConnectionString = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        string cmdText = "UPDATE Product SET " +
            "Title='" + sName + "'," +
            "Showorder=" + nOrder + " WHERE ProductID=" + nLeiID;
        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 DeleteItem(int nLeiitemID)
    {
        string myConnectionString = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        string cmdText = "DELETE FROM Product WHERE ProductID=" + nLeiitemID + " OR ParrentID=" + nLeiitemID;
        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 AddItem(string sName, int nOrder, int nFolder)
    {
        string myConnectionString = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        string cmdText = "INSERT INTO Product(Title,Showorder,Folder)VALUES('" +
            sName + "'," +
            nOrder + "," +
            nFolder + ")";
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
    }
    //显示排序
    protected string Myorder(int nOrder)
    {
        nOrder -= 10;
        return nOrder.ToString();
    }
    //更新
    protected void LeiGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int order = -1;
        int nLeiID = Int32.Parse(LeiGridView.DataKeys[e.RowIndex].Value.ToString());
        TextBox name = (TextBox)LeiGridView.Rows[e.RowIndex].FindControl("txtName");
        TextBox xuhao = (TextBox)LeiGridView.Rows[e.RowIndex].FindControl("txtXuhao");
        string myname = name.Text.ToString().Trim();
        if (Int32.TryParse(xuhao.Text.ToString(), out order) == false)
        {
            Response.Write("<Script>alert('在排序中只能输入(0-9)的数字！')</Script>");
            return;
        }
        if (order < 0||order>9)
        {
            Response.Write("<Script>alert('在排序中只能输入(0-9)数字！')</Script>");
            return;
        }
        if (myname.Length<1)
        {
            Response.Write("<Script>alert('名称不能为空！')</Script>");
            return;
        }
        order += 10;
        UpdateLei(myname, order, nLeiID);
        BindLeiGridView();
    }
    //删除操作
    protected void LeiGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ImageButton Mybutton = (ImageButton)LeiGridView.Rows[e.RowIndex].FindControl("delBtn");
        int nLeiID = Int32.Parse(Mybutton.CommandArgument.ToString());
        DeleteItem(nLeiID);
        BindLeiGridView();
    }
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = txtAddlei.Text.ToString().Trim();
        int order = -1;
        if (Int32.TryParse(txtAddxuhao.Text.ToString().Trim(), out order) == false)
        {
            Response.Write("<Script>alert('序号要求为(0-9)数字！')</Script>");
            return;
        }
        if (order < 0 || order > 9)
        {
            Response.Write("<Script>alert('在排序中只能输入(0-9)数字！')</Script>");
            return;
        }
        if (name.Length < 1)
        {
            Response.Write("<Script>alert('类别名称不能为空！')</Script>");
            return;
        }
        order += 10;
        AddItem(name, order, 1);
        BindLeiGridView();
        txtAddlei.Text = "";
        txtAddxuhao.Text = "";
    }
    
    //翻页
    protected void LeiGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LeiGridView.PageIndex = e.NewPageIndex;
        BindLeiGridView();
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }

        }
    }
}
