using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.OleDb;

public partial class BackCenter_Data_SetGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 过期和权限

        PageFunc.IfLogin(this.Page);
        //PubPageFunc.PageHavePurview(this.Page, "2001");

        #endregion

        if (!IsPostBack)
        {
            #region 页面赋值 取栏目信息
            if (Request["ClassId"] != null)
            {
                string strSql_Class = "select [ID],[ClassName] from NT_Class where ID=" + Request["ClassId"] + "";
                OleDbDataReader sdr_Class = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql_Class, null);
                if (sdr_Class.HasRows)
                {
                    while (sdr_Class.Read())
                    {
                        this.lab_ClassName1.Text = sdr_Class["ClassName"].ToString();
                    }
                }
                sdr_Class.Close();
            }
            #endregion

            //取所有组合
            string sql = "select [ID],[Type],[Name],[OrderNum] from NT_Group order by OrderNum asc,ID desc";
            WebControlsFunc.BindToCheckBoxList(this.cbl_GroupId, sql, "Name", "Id");

            //修改
            if (Request["DataId"] != null)
            {
                //页面赋值
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" select [ID],[GroupId],[DataId] from NT_DataGroup ");
                strSql.Append(" where DataId=@DataId");
                OleDbParameter[] parameters = { new OleDbParameter("@DataId", OleDbType.Integer, 4) };
                parameters[0].Value = Request["DataId"];

                OleDbDataReader sdr = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql.ToString(), parameters);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        //循环选中
                        for (int i = 0; i < cbl_GroupId.Items.Count; i++)
                        {
                            if (cbl_GroupId.Items[i].Value == sdr["GroupId"].ToString())
                            {
                                cbl_GroupId.Items[i].Selected = true;
                            }
                        }
                    }
                    this.lab_NO.Visible = false;
                }
                sdr.Close();
            }

        }

    }


    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] sqllist = new string[1];
        sqllist[0] = "delete from NT_DataGroup where DataId=" + Request["DataId"] + "";

        for (int i = 0; i < cbl_GroupId.Items.Count; i++)
        {
            if (cbl_GroupId.Items[i].Selected == true)
            {
                string sql = "INSERT INTO NT_DataGroup ([DataId] ,[GroupId]) VALUES (" + Request["DataId"] + "," + cbl_GroupId.Items[i].Value + ")";
                sqllist = StringFunc.ArrayAddItem(sqllist, sql);
            }
        }
        DbHelperOleDb.ExecuteSqlTran(sqllist);

        JavaScriptFunc.Alert(this.Page, "保存成功");
    }
}
