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 WTOSYSadmin_news_newsmag : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (Request.QueryString["doing"] != null)
        {
            Doadmin.Exec("delete from Package where id=" + Request.QueryString["id"]);
        }
        if (!IsPostBack)
        {
            this.databind();
        }
        if (Request.QueryString["id"] != null & Request.QueryString["doing2"] != null)
        {
            string doing = Request.QueryString["doing2"];
            string sql1 = "";
            string zt = Request.QueryString["zt"].ToString().Trim();
            switch (doing)
            {
                case "isfp":
                    if (zt == "y")
                    {
                        sql1 = "update Package set isfp='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update Package set isfp='y' where id=" + Request.QueryString["id"];
                    }

                    break;
                case "zd":
                    sql1 = "update Package set adddate='" + DateTime.Now.ToString() + "' where id=" + Request.QueryString["id"];
                    break;

            }
            Doadmin.Execsql(sql1);
            databind();
        }

    }
    protected void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sad = new SqlDataAdapter("select * from Package order by adddate desc", conn);
        DataSet ds = new DataSet();
        sad.Fill(ds, "cu");
        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
        if (GridView1.Rows.Count == 0)
        {
            this.nors.Visible = true;
            this.t1.Visible = false;
        }
        else
        {
        this.Label1.Text = "当前记录数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.GridView1.PageCount + "</b></font>页显示.";
        SqlCommand smd2;
        SqlDataReader sdr2;
        LinkButton isfb;
        string txt="";
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            smd2 = new SqlCommand("select isfp from Package where id=" + GridView1.DataKeys[i].Value, conn);
            if (Convert.ToString(smd2.ExecuteScalar())== "y")
            {
                isfb = (LinkButton)GridView1.Rows[i].FindControl("isfp");
                isfb.Text = "√";
                isfb.PostBackUrl = "Package.aspx?id=" + GridView1.DataKeys[i].Value + "&zt=y&doing2=isfp";
            }
            if (Convert.ToString(smd2.ExecuteScalar()) == "n")
            {
                isfb = (LinkButton)GridView1.Rows[i].FindControl("isfp");
                isfb.Text = "×";
                isfb.PostBackUrl = "Package.aspx?id=" + GridView1.DataKeys[i].Value + "&zt=n&doing2=isfp";
            }
            isfb = (LinkButton)GridView1.Rows[i].FindControl("at");
            isfb.Text = "置顶";
            isfb.PostBackUrl = "Package.aspx?id=" + GridView1.DataKeys[i].Value + "&zt=n&doing2=zd";

        }
        }
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((HyperLink)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from Package where id=" + GridView1.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        this.databind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }

}
