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_vipmag_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            databind();
        }
    }
    private void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from DataMasterAdditionalControl where mid<>0 and zt='y'", conn);
        DataSet ds = new DataSet();
        sda.Fill(ds,"cu");
        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
        if (GridView1.Rows.Count == 0)
        {
            this.ms.Visible = true;
        }
        else
        { 
           this.ms.Text = "当前记录数为<font color='red'><b>" + ds.Tables["cu"].DefaultView.Count + "</b></font>.分为<font color='red'><b>" + this.GridView1.PageCount + "</b></font>页显示.";
        }
        CheckBox chk;
        SqlCommand sad2;
        SqlDataReader sdr2;
        sad2 = new SqlCommand("select gnid from UserAdmin where id=" +Request.QueryString["id"], conn);
        string isok = Convert.ToString(sad2.ExecuteScalar()).Trim();

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            sad2 = new SqlCommand("select bigid,smallid from DataMasterAdditionalControl where id=" + GridView1.DataKeys[i].Value.ToString(), conn);
            sdr2 = sad2.ExecuteReader();
            sdr2.Read();
            string mot=GridView1.DataKeys[i].Value.ToString() + "|" + sdr2["smallid"].ToString() + "|" + sdr2["bigid"].ToString() + "$";
            sdr2.Close();
            if (isok.Length - isok.Replace(mot,"").Length > 0)
            {
                chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                chk.Checked = true;
            }
        }
        conn.Close();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行

            e.Row.Attributes.Add("ondblclick", "location.href='vipedit.aspx?id=" + GridView1.DataKeys[g].Value + "'");
            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
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string motxt = "";
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd;
        SqlDataReader sdr;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                if (GridView1.DataKeys[i].Value.ToString() != "10000001")
                {
                     smd = new SqlCommand("select bigid,smallid from DataMasterAdditionalControl where id=" + GridView1.DataKeys[i].Value.ToString(), conn);
                     sdr = smd.ExecuteReader();
                     sdr.Read();
                     motxt += GridView1.DataKeys[i].Value.ToString() + "|" +sdr["smallid"].ToString() + "|" + sdr["bigid"].ToString()+ "$";
                     sdr.Close();
                }
            }
        }
        conn.Close();
        string sqlstr = "update UserAdmin set gnid='"+motxt+"' where id="+Request.QueryString["id"].Trim();
        if (Doadmin.Exec(sqlstr))
        {
            Response.Write("<script>alert('操作成功；');location.href='SysRight.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('操作失败；');location.href='sysrightset.aspx?id="+Request.QueryString["id"].Trim()+"'</script>");
        }
    }
    
}
