using System;
using System.Data;
using System.Configuration;
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_Diary : System.Web.UI.Page
{
    SqlConnection one = conn.sqlconn();   //建立连接数据库对象
    sqloper sqloper = new sqloper();
    int gvEditIndex = -1;
    string connect_sql;

    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)
    {

        //wormadmin_search search_form = (wormadmin_search)Context.Handler;
        //Response.Write(search_form.txt_date1 + "sdfasd");
        //if (Request.QueryString["search"] != "1")
        //{
        //    connect_sql = "";
        //}
        //Response.Write(Request.QueryString["search"]);
        //Response.Write(connect_sql);
        connect_sql = tsql.Text;
        if (!IsPostBack)
        {
            if (Request.QueryString["search"] == "1")
            {
                connect_sql = req();
                tsql.Text = connect_sql;
            }
            //string connect_sql = Request.QueryString["connect_sql"];
            gvbind("select * from dairy " + connect_sql + " order by id desc", one, myGrid, AspNetPager1.PageSize);
        }
    }

    //接受搜索页面的值,然后进行sql语句合拼处理
    protected string req()
    {
        string reqstr = "";
        string txttitle = Request["txttitle"];
        string txtclass = Request["txtclass"];
        string bit1 = Request["bit1"];
        string bit2 = Request["bit2"];
        string date1 = Request["date1"];
        string date2=Request["date2"];
        int cktop;
        if (Request["cktop"] == null)
        {
            cktop = 0;
        }
        else
        {
            cktop = 1;
        }
        string temptitle = Request["temptitle"];
        append_sql apsql = new append_sql();
        reqstr = apsql.sql_str(temptitle, txttitle, txtclass, bit1, bit2, date1,date2, cktop);
        return reqstr;
    }

    //字符串截取
    protected string strcut(string mystr, int len)
    {
        stringdo str = new stringdo();
        string restr = str.CutString(mystr, len);
        return restr;
    }

    //短日期处理
    protected string short_time(object times)
    {
        //return DateTime.Parse(Convert.ToDateTime(times)).ToString("yyyy-MM-dd");
        stringdo shortime = new stringdo();
        return shortime.short_time(times);
    }

    #region 评论列表plGrid表操作-----------------------------------------------------------------

    //myGrid行绑定事件
    //实行评论表的数据绑定
    protected void plbound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            GridView oGridView = (GridView)e.Row.FindControl("plGrid");
            if (oGridView != null)
            {
                oGridView.EditIndex = gvEditIndex;     //编辑哪一行，行标
                gvbind2("select * from redai where drid=" + myGrid.DataKeys[e.Row.RowIndex].Value + "  order by id desc", one, oGridView, AspNetPager1.PageSize);
            }
        }

    }

    //设置评论列表Gridview表绑定
    protected void gvbind2(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader rs = cmd.ExecuteReader();
        try
        {
            gvid.DataSource = rs;
            gvid.DataBind();
        }
        catch (System.Exception ee)
        {
            Response.Write("gvbind2" + ee.Message.ToString());
        }
        finally
        {
            cmd.Dispose();
            rs.Dispose();
        }
    }

    //评论表的数据删除
    protected void pldelete(object sender, GridViewDeleteEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        sqloper.dodata("delete from redai where id=" + gvTemp.DataKeys[e.RowIndex].Value, one);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('删除评论成功!');</script>");
        gvbind("select * from dairy " + connect_sql + " order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //评论表的数据编辑
    protected void pledit(object sender, GridViewEditEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvEditIndex = e.NewEditIndex;
        gvbind("select * from dairy " + connect_sql + " order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //评论表数据取消
    protected void plcancel(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvEditIndex = -1;
        gvbind("select * from dairy " + connect_sql + " order by id desc", one, myGrid, AspNetPager1.PageSize);
        //gvbind("select * from dairy order by id desc", one, myGrid, AspNetPager1.PageSize);
        //dobing2();
    }

    //plGrid日记评论表的更新操作
    protected void plupdating(object sender, GridViewUpdateEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        string drwriter = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("drwriter")).Text;
        string drcontent = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("drcontent")).Text;
        int drunable = Convert.ToInt16(((CheckBox)gvTemp.Rows[e.RowIndex].FindControl("drunable")).Checked);
        string drtime = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("drtime")).Text;

        string sql = "update redai set drwriter='" + drwriter + "',drcontent='" + drcontent + "',drunable=" + drunable + ",drtime='" + drtime + "' where id=" + gvTemp.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        gvEditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新成功!');</script>");
        gvbind("select * from dairy " + connect_sql + " order by id desc", one, myGrid, AspNetPager1.PageSize);
    }
    #endregion----------------------------------------------------------------------------------

    #region 日记表Gridview表操作----------------------------------------------------------------
    //数据绑定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("gvbind" + ee.Message.ToString());
        }
        finally
        {
            ds.Dispose();
            adapter.Dispose();
            conn.Close();
        }
    }

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

    //gridview删除链接
    //单个删除,已取消这个功能
    //这个列加进去即可:<asp:CommandField HeaderText="删除" ShowDeleteButton="True" DeleteText="&lt;div  onclick=&quot;JavaScript:return confirm('确定删除吗？')&quot;&gt;删除&lt;/div&gt;"/>
    protected void mydelete(object sender, GridViewDeleteEventArgs e)
    {
        sqloper.dodata("delete from dairy where id=" + myGrid.DataKeys[e.RowIndex].Value, one);
        gvbind("select * from dairy " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

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

        myGrid.EditIndex = e.NewEditIndex;
        gvbind("select * from dairy " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
        try
        {
            one.Open();
            string sql = "select distinct dclass from classdai";
            SqlCommand cmd = new SqlCommand(sql, one);
            SqlDataReader rs = cmd.ExecuteReader();
            int i = 0;
            while (rs.Read())
            {

                ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).Items.Add(new ListItem(rs["dclass"].ToString(), rs["dclass"].ToString()));
                if (((TextBox)myGrid.Rows[e.NewEditIndex].FindControl("tempclass")).Text == rs["dclass"].ToString())
                {
                    ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).SelectedIndex = i;
                }
                i = i + 1;
            }
        }
        catch (System.Exception ee)
        {
            Response.Write("myedit" + ee.Message.ToString());
        }
        finally
        {
            one.Close();
        }
    }

    //gridview更新数据事件
    protected void myupdate(object sender, GridViewUpdateEventArgs e)
    {
        //string uptitle=(myGrid.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text.ToString();
        //Response.Write(uptitle);
        //string idtemp = (myGrid.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text.ToString();
        //string uptitle = myGrid.Rows[e.RowIndex].Cells["dtitle"].Controls[0].ToString();
        string uptitle = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("dtitle")).Text;
        string dclass = ((DropDownList)myGrid.Rows[e.RowIndex].FindControl("dlclass")).SelectedValue.ToString();
        int dbit = Convert.ToInt16(((TextBox)myGrid.Rows[e.RowIndex].FindControl("dbit")).Text);
        string dtime = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("dtime")).Text;
        string dwriter = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("dwriter")).Text;
        int dtop = Convert.ToInt16(((CheckBox)myGrid.Rows[e.RowIndex].FindControl("top")).Checked);

        string sql = "update dairy set dtitle='" + uptitle + "',dclass='" + dclass + "',dbit=" + dbit + ",dtime='" + dtime + "',dwriter='" + dwriter + "',dtop=" + dtop + " where id=" + myGrid.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        myGrid.EditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新成功!');</script>");
        gvbind("select * from dairy " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //gridview取消编辑事件
    protected void mycancel(object sender, GridViewCancelEditEventArgs e)
    {
        myGrid.EditIndex = -1;
        gvbind("select * from dairy " + connect_sql + "  order by id 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 myKeyid;
        CheckBox mycheck = new CheckBox();
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                sql = "DELETE FROM dairy where id=" + myKeyid;
                sqloper.dodata(sql, one);
                sql = "DELETE FROM redai where drid=" + myKeyid;
                sqloper.dodata(sql, one);
            }
        }
        gvbind("select * from dairy " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //全选checkbox按钮
    protected void delall_CheckedChanged(object sender, EventArgs e)
    {

        //if (((CheckBox)myGrid.FindControl("delall")).Checked == true)
        //{
        //    int i;
        //    for (i = 0; i < ((CheckBox)myGrid.FindControl("del")).Controls.Count; i++)
        //    {
        //        ((CheckBox)myGrid.FindControl("del")).Checked = true;

        //    }
        //}
        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;
            }
        }

    }

    //全选button按钮
    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;
            }
        }
    }
    #endregion--------------------------------------------------------------------------------------
    

}
