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 wormadmin_dairy_class : System.Web.UI.Page
{
    sqloper sqloper = new sqloper();
    SqlConnection one = conn.sqlconn();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["superad"] == null || Session["superpw"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Page.Theme = "SkinFile";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
        }

    }

    //数据绑定GridView
    //sql:数据库查询语句,conn:数据库连接,gvid:gridview控件的id,pssize:一页中数据的多少
    protected void gvbind(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        PagedDataSource ps = new PagedDataSource();
        try
        {
            conn.Open();
            adapter.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;              //ps分页数据源设置
            ps.AllowPaging = true;                                       //允许分页
            ps.PageSize = pssize;                                       //页面数据大小
            ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;    //当前页面指针
            AspNetPager1.RecordCount = ds.Tables["order"].Rows.Count;  //记录总行数赋值给控件AspNetPager1

            gvid.DataSource = ps;
            gvid.DataBind();
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            ds.Dispose();
            adapter.Dispose();
            conn.Close();
        }
    }


    //gridview编辑链接
    protected void myedit(object sender, GridViewEditEventArgs e)
    {
        //用NewEidIndex取得当前编辑的行号，然后获取gridviewrow对象

        myGrid.EditIndex = e.NewEditIndex;
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview更新数据事件
    protected void myupdate(object sender, GridViewUpdateEventArgs e)
    {
        string dclass = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("dclass")).Text;
        string dclasstemp = ((Label)myGrid.Rows[e.RowIndex].FindControl("dclasstemp")).Text;
        //更新classdai表的分类
        string sql = "update classdai set dclass='" + dclass + "' where id=" + myGrid.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        //更新dairy表的分类
        string sql2 = "update dairy set dclass='" + dclass + "'  where dclass='" + dclasstemp + "'";
        sqloper.dodata(sql2, one);
        myGrid.EditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新成功!');</script>");
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);

    }

    //gridview取消编辑事件
    protected void mycancel(object sender, GridViewCancelEditEventArgs e)
    {
        myGrid.EditIndex = -1;
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
    }

    //翻页数据绑定事件
    protected void pagego(object sender, EventArgs e)
    {
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
    }

    //选中删除按钮事件
    protected void btdel_Click(object sender, EventArgs e)
    {
        //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>confirm('是否确认删除此留言?');</script>");
        //Response.Write("<SCRIPT LANGUAGE='javascript'>if(confirm('是否确认删除此留言?')){return true;}return false;</SCRIPT>");
        int i;
        string sql;
        string sql2;
        string myKeyid;
        CheckBox mycheck = new CheckBox();
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            string dclasstemp = ((Label)myGrid.Rows[i].FindControl("dclasstemp")).Text;
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                //删除classdai表的分类
                sql = "DELETE FROM classdai where id=" + myKeyid;
                sqloper.dodata(sql, one);
                sql2 = "DELETE FROM dairy where dclass='" + dclasstemp+"'";
                sqloper.dodata(sql2, one);

            }
        }
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
    }

    protected void delall_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked == true)
            {
                ch.Checked = true;
            }
            else
            {
                ch.Checked = false;
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (ch.Checked == false)
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = true;
                ch.Checked = true;
            }
            else
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = false;
                ch.Checked = false;
            }
        }
    }

    protected void add_Click(object sender, EventArgs e)
    {
        int maxsort = 0;
        string tbclass = addclass.Text;
        SqlDataReader rs = sqloper.readsql("select max(dsort)+1 from classdai", one);
        try
        {
            if (rs.Read())
            {
                maxsort = Convert.ToInt16(rs[0]);
            }
        }
        catch { }
        finally
        {
            rs.Dispose();
            one.Close();
        }
        string sql = "insert into classdai(dclass,dsort) values('" + tbclass + "'," + maxsort + ")";
        sqloper.dodata(sql, one);
        gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('添加成功!');</script>");
    }

    //上下移动排序操作
    protected void piccommand(object sender, GridViewCommandEventArgs e)
    {
        //上移操作
        if (e.CommandName == "shang")
        {
            int psort2 = -1;    //上移到的排序号码
            int id1 = 0;          //要上移的id号
            int id2 = 0;          //上移到的id号
            int therow = Convert.ToInt16(e.CommandArgument);     //当前操作行
            int psort1 = Convert.ToInt16(((Label)myGrid.Rows[therow].FindControl("dsorttemp")).Text);  //要上移的排序号码
            try
            {
                SqlDataReader rs = sqloper.readsql("select * from classdai where dsort>" + psort1, one);
                ArrayList arrpic = new ArrayList();     //建立动态数组,求比当前要移动的数大的第一个数
                id1 = Convert.ToInt16(myGrid.DataKeys[therow].Value);
                while (rs.Read())
                {
                    arrpic.Add(Convert.ToInt16(rs["dsort"]));      //把排序数都添加到动态数组
                }
                rs.Dispose();
                arrpic.Sort();    //排序
                psort2 = Convert.ToInt16(arrpic[0]);    //得到了上移到的排序号码
                id2 = Convert.ToInt16(sqloper.loaddb("select * from classdai where dsort=" + psort2, "id"));   //寻找回该排序号的id号
                arrpic.Clear();
            }
            catch { }
            finally
            {
                one.Close();
            }
            if (psort1 != -1 && psort2 != -1)
            {
                sqloper.dodata("update classdai set dsort=" + psort1 + " where id=" + id2, one);   //把要上移的和上移到的两个记录数交换
                sqloper.dodata("update classdai set dsort=" + psort2 + " where id=" + id1, one);
                gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
            }
        }
        //下移操作
        if (e.CommandName == "xia")
        {
            int psort2 = -1;    //下移到的排序号码
            int id1 = 0;          //要下移的id号
            int id2 = 0;          //下移到的id号
            int therow = Convert.ToInt16(e.CommandArgument);     //当前操作行
            int psort1 = Convert.ToInt16(((Label)myGrid.Rows[therow].FindControl("dsorttemp")).Text);  //要下移的排序号码
            try
            {
                SqlDataReader rs = sqloper.readsql("select * from classdai where dsort<" + psort1, one);
                ArrayList arrpic = new ArrayList();     //建立动态数组,求比当前要移动的数小的第一个数
                id1 = Convert.ToInt16(myGrid.DataKeys[therow].Value);
                while (rs.Read())
                {
                    arrpic.Add(Convert.ToInt16(rs["dsort"]));      //把排序数都添加到动态数组
                }
                rs.Dispose();
                arrpic.Sort();    //排序
                psort2 = Convert.ToInt16(arrpic[arrpic.Count - 1]);    //得到了下移到的排序号码,arrpic.Count-1为数组最后那个数
                id2 = Convert.ToInt16(sqloper.loaddb("select * from classdai where dsort=" + psort2, "id"));   //寻找回该排序号的id号
                arrpic.Clear();
            }
            catch { }
            finally
            {
                one.Close();
            }
            if (psort1 != -1 && psort2 != -1)
            {
                sqloper.dodata("update classdai set dsort=" + psort1 + " where id=" + id2, one);   //把要下移的和下移到的两个记录数交换
                sqloper.dodata("update classdai set dsort=" + psort2 + " where id=" + id1, one);
                gvbind("select * from classdai order by dsort desc", one, myGrid, AspNetPager1.PageSize);
            }
        }
    }
}
