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_SetFile : 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
            BindData();
        }
    }

    //绑定
    public void BindData()
    {

        //查询语句的头部
        string sql_SelectFirst = "select ";
        sql_SelectFirst += " [ID],[DataId],[FileName],[FilePath],[OrderNum]";
        sql_SelectFirst += " from NT_DataFile";
        sql_SelectFirst += " where DataId=" + Request["DataId"] + "";

        //排序
        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_DataFile ");
        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)
    {
        string str_DataId = Request["DataId"];
        string str_FileName = this.txt_FileName.Text;
        string str_FilePath = string.Empty;

        #region 保存文件
        if (this.fu_FilePath != null && this.fu_FilePath.FileName.Length > 0)
        {
            //赋值
            str_FilePath = FileFunc.CreateFile(this.Page, fu_FilePath);
        }
        #endregion

        string str_OrderNum = this.txt_OrderNum.Text;
        #region 操作数据库

        //执行语句

        StringBuilder strSql = new StringBuilder();
        strSql.Append("insert into NT_DataFile(");
        strSql.Append("[DataId],[FileName],[FilePath],[OrderNum])");
        strSql.Append(" values (");
        strSql.Append("@DataId,@FileName,@FilePath,@OrderNum)");
        OleDbParameter[] parameters = {
					new OleDbParameter("@PropertyNameId", OleDbType.Integer),
					new OleDbParameter("@Name", OleDbType.VarChar),
                    new OleDbParameter("@Name", OleDbType.VarChar),
					new OleDbParameter("@OrderNum", OleDbType.Integer)};
        parameters[0].Value = str_DataId;
        parameters[1].Value = str_FileName;
        parameters[2].Value = str_FilePath;
        parameters[3].Value = str_OrderNum;
        DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);

        this.txt_FileName.Text = "";
        this.txt_OrderNum.Text = "";
        JavaScriptFunc.Alert(this.Page, "添加成功");
        #endregion
        BindData();

    }

}
