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_Products_ProductAttribute : 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["id"] != null & Request.QueryString["doing"] != null)
        {
            string doing = Request.QueryString["doing"];
            string sql1 = "";
            string zt = Request.QueryString["zt"].ToString().Trim();
            switch (doing)
            {
                case "isfp":
                    if (zt == "y")
                    {
                        sql1 = "update ProductInfo set ISPublished='n' where id=" + Request.QueryString["id"];
                    }
                    else
                    {
                        sql1 = "update ProductInfo set ISPublished='y' where id=" + Request.QueryString["id"];
                    }

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

        }


        if (!IsPostBack)
        {
            this.databind();
        }
     
    }
   public void databind()
    {
        LinkButton isfb,isnew, ishot, isT, isTprice;
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sdr=new SqlDataAdapter("select * from ProductInfo order by AddDate desc",conn);
        DataSet ds=new DataSet();
        sdr.Fill(ds,"cu");
        this.Productmag.DataSource = ds;
        this.Productmag.DataKeyNames = new string[] {"id"};
        this.Productmag.DataBind();
        this.Label1.Text = "当前会员数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.Productmag.PageCount + "</b></font>页显示.";

       if (Productmag.Rows.Count == 0)
        {
            this.mslable.Visible = true;
        }
       //图片显示[功能图片]
        SqlCommand smd2;
        SqlDataReader sdr2;
        for (int i = 0; i <= Productmag.Rows.Count - 1; i++)
        {
            DataRowView mydrv = ds.Tables["cu"].DefaultView[i];

            smd2 = new SqlCommand("select ISPublished,IsNew,IsHot,IsRecommended,IsSpecials from ProductInfo where id=" + Productmag.DataKeys[i].Value, conn);
            sdr2 = smd2.ExecuteReader();
            sdr2.Read();

            //是否发布
            if (sdr2.GetString(0)== "y")
            {
                isfb = (LinkButton)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "√";
                isfb.PostBackUrl = "ProductsList2.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=y&doing=isfp";
            }
            if (sdr2.GetString(0) == "n")
            {
                isfb = (LinkButton)Productmag.Rows[i].FindControl("isfp");
                isfb.Text = "×";
                isfb.PostBackUrl = "ProductsList2.aspx?id=" + Productmag.DataKeys[i].Value + "&zt=n&doing=isfp";
            }
            sdr2.Close();
        }
       conn.Close();
   }
    protected void Productmag_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < Productmag.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[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }

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

            if (cbox.Checked == true)
            {
                string sqlstr = "delete from ProductInfo where id=" + Productmag.DataKeys[i].Value;
                Doadmin.Execsql(sqlstr);
            }
        }
        this.databind();
    }
}
