protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                BindGrid();

                if (!String.IsNullOrEmpty(Request.QueryString["tyId"]))
                {
                    ddlfatherId.SelectedValue = Request.QueryString["tyId"];
                    ddlfatherId.Enabled       = false;
                }
                if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    ddlfatherId.Enabled = true;
                    Maticsoft.BLL.tPicture   bll   = new Maticsoft.BLL.tPicture();
                    Maticsoft.Model.tPicture model = bll.GetModel(int.Parse(Request.QueryString["Id"]));

                    txtTile.Text   = model.Name;
                    txtRemark.Text = model.Remark;

                    ddlfatherId.SelectedValue = model.PicTyID.ToString();
                    txtSort.Text           = model.Sort.ToString();
                    hfEditorInitValue.Text = "<img src=" + "'" + model.Code + "'" + "/>";
                }
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.tPicture model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update tPicture set ");
            strSql.Append("PicTyID=@PicTyID,");
            strSql.Append("Name=@Name,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("ImageUrl=@ImageUrl,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("Code=@Code");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@PicTyID",  SqlDbType.Int,        4),
                new SqlParameter("@Name",     SqlDbType.NVarChar, 500),
                new SqlParameter("@Sort",     SqlDbType.Int,        4),
                new SqlParameter("@ImageUrl", SqlDbType.NVarChar,  -1),
                new SqlParameter("@Remark",   SqlDbType.NVarChar,  -1),
                new SqlParameter("@Code",     SqlDbType.NVarChar, 100),
                new SqlParameter("@Id",       SqlDbType.Int, 4)
            };
            parameters[0].Value = model.PicTyID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Sort;
            parameters[3].Value = model.ImageUrl;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.Code;
            parameters[6].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        protected void Grid_RowCommand(object sender, GridCommandEventArgs e)
        {
            int artId = GetSelectedDataKeyID(Grid);


            if (e.CommandName == "Delete")
            {
                Maticsoft.BLL.tPicture BLL = new Maticsoft.BLL.tPicture();

                Maticsoft.Model.tPicture model = BLL.GetModel(artId);

                string directoryPath = Server.MapPath(model.Code);
                File.Delete(directoryPath);

                BLL.Delete(artId);
                GetView();
            }
            if (e.CommandName == "Edit")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference("PicturesEdit.aspx?Id=" + artId));
            }
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.tPicture model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into tPicture(");
            strSql.Append("PicTyID,Name,Sort,ImageUrl,Remark,Code)");
            strSql.Append(" values (");
            strSql.Append("@PicTyID,@Name,@Sort,@ImageUrl,@Remark,@Code)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@PicTyID",  SqlDbType.Int,        4),
                new SqlParameter("@Name",     SqlDbType.NVarChar, 500),
                new SqlParameter("@Sort",     SqlDbType.Int,        4),
                new SqlParameter("@ImageUrl", SqlDbType.NVarChar,  -1),
                new SqlParameter("@Remark",   SqlDbType.NVarChar,  -1),
                new SqlParameter("@Code",     SqlDbType.NVarChar, 100)
            };
            parameters[0].Value = model.PicTyID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Sort;
            parameters[3].Value = model.ImageUrl;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.Code;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);

            if (obj == null)
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
 /// <summary>
 /// 得到一个对象实体
 /// </summary>
 public Maticsoft.Model.tPicture DataRowToModel(DataRow row)
 {
     Maticsoft.Model.tPicture model = new Maticsoft.Model.tPicture();
     if (row != null)
     {
         if (row["Id"] != null && row["Id"].ToString() != "")
         {
             model.Id = int.Parse(row["Id"].ToString());
         }
         if (row["PicTyID"] != null && row["PicTyID"].ToString() != "")
         {
             model.PicTyID = int.Parse(row["PicTyID"].ToString());
         }
         if (row["Name"] != null)
         {
             model.Name = row["Name"].ToString();
         }
         if (row["Sort"] != null && row["Sort"].ToString() != "")
         {
             model.Sort = int.Parse(row["Sort"].ToString());
         }
         if (row["ImageUrl"] != null)
         {
             model.ImageUrl = row["ImageUrl"].ToString();
         }
         if (row["Remark"] != null)
         {
             model.Remark = row["Remark"].ToString();
         }
         if (row["Code"] != null)
         {
             model.Code = row["Code"].ToString();
         }
     }
     return(model);
 }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.tPicture GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 Id,PicTyID,Name,Sort,ImageUrl,Remark,Code from tPicture ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Id", SqlDbType.Int, 4)
            };
            parameters[0].Value = Id;

            Maticsoft.Model.tPicture model = new Maticsoft.Model.tPicture();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return(DataRowToModel(ds.Tables[0].Rows[0]));
            }
            else
            {
                return(null);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Maticsoft.BLL.tPicture bll = new Maticsoft.BLL.tPicture();

            string content = Request.Form["editor1"] == null ? "" : Request.Form["editor1"];

            if (string.IsNullOrEmpty(content))
            {
                Alert.ShowInTop("图片为空!"); return;
            }
            string[] picList = GetHtmlImageUrlList(content);
            if (picList.Length <= 0)
            {
                Alert.ShowInTop("图片为空!"); return;
            }


            if (!String.IsNullOrEmpty(Request.QueryString["tyId"]))
            {
                int k = 0;
                for (int i = 0; i < picList.Length; i++)
                {
                    Maticsoft.Model.tPicture model = new Maticsoft.Model.tPicture();
                    model.Code     = picList[i];
                    model.Name     = txtTile.Text;
                    model.Remark   = txtRemark.Text;
                    model.ImageUrl = txtUrl.Text;
                    model.PicTyID  = int.Parse(ddlfatherId.SelectedValue);
                    model.Sort     = int.Parse(txtSort.Text);

                    if (bll.Add(model) > 0)
                    {
                        k++;
                    }
                }
                if (k > 0)
                {
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
                else
                {
                    Alert.ShowInTop("添加失败!"); return;
                }
            }
            if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                if (picList.Length > 1)
                {
                    Alert.ShowInTop("请上传一张图片!"); return;
                }
                Maticsoft.Model.tPicture model = bll.GetModel(int.Parse(Request.QueryString["Id"]));
                if (picList[0] != model.Code)
                {
                    string directoryPath = Server.MapPath(model.Code);
                    File.Delete(directoryPath);
                }
                model.Code     = picList[0];
                model.Name     = txtTile.Text;
                model.Remark   = txtRemark.Text;
                model.ImageUrl = txtUrl.Text;
                model.PicTyID  = int.Parse(ddlfatherId.SelectedValue);
                model.Sort     = int.Parse(txtSort.Text);
                bll.Update(model);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }