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_Class_SetPropertyName : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 过期和权限

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

        #endregion

        #region 第一次页面加载
        if (!IsPostBack)
        {
            //属性复选框
            string sql = "select Id,Name from NT_PropertyName order by OrderNum asc";
            WebControlsFunc.BindToCheckBoxList(this.cbl_PropertyNameId, sql, "Name", "Id");

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

                OleDbDataReader sdr = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql.ToString(), parameters);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        for (int i = 0; i < cbl_PropertyNameId.Items.Count; i++)
                        {
                            if (cbl_PropertyNameId.Items[i].Value == sdr["PropertyNameId"].ToString())
                            {
                                cbl_PropertyNameId.Items[i].Selected = true;
                            }
                        }
                    }
                }
                sdr.Close();
            }

        }
        #endregion
    }


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

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

        JavaScriptFunc.AlertAndRedirect(this.Page, "保存成功", "ClassIndex.aspx?PageIndex=true&Conditions=true");
    }
}
