using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Text;
public partial class BackCenter_PropertyName_SetPropertyValue : 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)
        {
            BindData();
        }
    }

    //绑定
    public void BindData()
    {

        //查询语句的头部
        string sql_SelectFirst = "select ";
        sql_SelectFirst += " Id,";
        sql_SelectFirst += " Name,";
        sql_SelectFirst += " OrderNum";
        sql_SelectFirst += " from NT_PropertyValue";
        sql_SelectFirst += " where PropertyNameId=" + Request["PropertyNameId"] + "";

        //排序
        string sql_SelectLast = " order by OrderNum asc,Id desc";


        //查询条件
        string sql_Conditions = string.Empty;



        //查询语句
        string str_Select = sql_SelectFirst + sql_Conditions + sql_SelectLast;


        DataSet sda = DbHelperOleDb.ExecuteDataSet(CommandType.Text, str_Select, null);
        GridView1.DataSource = sda.Tables[0];
        GridView1.DataBind();
        GridView1.Dispose();
    }

    //删除
    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {

        StringBuilder strSql = new StringBuilder();
        strSql.Append("delete from NT_PropertyValue ");
        strSql.Append(" where Id=@Id");
        OleDbParameter[] parameters = {
					new OleDbParameter("@ID",e.CommandArgument.ToString())
			};


        int rows = DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

        BindData();
    }

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {

        #region 操作数据库
        StringBuilder strSql1 = new StringBuilder();
        strSql1.Append("select count(*) from NT_PropertyValue");
        strSql1.Append(" where Name=@Name");
        OleDbParameter[] parameters1 = {
					new OleDbParameter("@Name", OleDbType.VarChar)};
        parameters1[0].Value = txt_Name.Text;


        object obj = DbHelperOleDb.ExecuteScalar(CommandType.Text, strSql1.ToString(), parameters1);
        if (obj.ToString() != "0")
        {
            JavaScriptFunc.Alert(this.Page, "名称不能重复！");
            return;
        }

        //执行语句

        StringBuilder strSql = new StringBuilder();
        strSql.Append("insert into NT_PropertyValue(");
        strSql.Append("[PropertyNameId],[Name],[OrderNum])");
        strSql.Append(" values (");
        strSql.Append("@PropertyNameId,@Name,@OrderNum)");
        OleDbParameter[] parameters = {
					new OleDbParameter("@PropertyNameId", OleDbType.Integer),
					new OleDbParameter("@Name", OleDbType.VarChar),
					new OleDbParameter("@OrderNum", OleDbType.Integer)};
        parameters[0].Value = int.Parse(Request["PropertyNameId"]);
        parameters[1].Value = txt_Name.Text;
        parameters[2].Value = txt_OrderNum.Text;

        DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

        this.txt_Name.Text = "";
        this.txt_OrderNum.Text = "";
        #endregion
        BindData();

    }
}
