public void ShowArticleInfo(string strID) { DAL.CMS.ArticleDAL dal = new DAL.CMS.ArticleDAL(); DataSet ds = dal.GetArticleDetail(strID); Model.CMS.CMS_Article model = DataConvert.DataRowToModel <Model.CMS.CMS_Article>(ds.Tables[0].Rows[0]); this.txtArticleTitle.Text = model.Title; this.hd_content.Value = model.Content; this.ddlCategory.SelectedIndex = this.ddlCategory.Items.IndexOf(this.ddlCategory.Items.FindByValue(model.Category)); this.ddlArticleIsTop.SelectedIndex = this.ddlArticleIsTop.Items.IndexOf(this.ddlArticleIsTop.Items.FindByValue(model.IsTop ? "是" : "否")); this.txtSummary.Text = model.Summary; this.txtOrder.Text = model.Order.ToString(); this.img0.Src = "../../" + model.Pic; if (strAction == "show") { this.btnReset.Visible = false; this.btnSave.Visible = false; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } //上传图标 string strIconFileName = string.Empty; //图标路径 string strIconSaveFileName = string.Empty; //网址路径 try { if (this.file0.PostedFile.FileName == "") { MessageBox.Show(this, "请选择上传文件!"); } else { if (!System.IO.Directory.Exists(Server.MapPath("~") + @"/Images")) { System.IO.Directory.CreateDirectory(Server.MapPath("~") + @"/Images"); } if (!System.IO.Directory.Exists(String.Format(@"{0}/Images/{1}", Server.MapPath("~"), Session["strSiteCode"].ToString()))) { System.IO.Directory.CreateDirectory(String.Format(@"{0}/Images/{1}", Server.MapPath("~"), Session["strSiteCode"].ToString())); } string orignalName = this.file0.PostedFile.FileName; //获取客户机上传文件的文件名 string extendName = orignalName.Substring(orignalName.LastIndexOf(".")); //获取扩展名 if (extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".png") { MessageBox.Show(this, "文件格式有误!"); return; }//检查文件格式 string newName = String.Format("{0}_{1}{2}", DateTime.Now.Millisecond, file0.PostedFile.ContentLength, extendName);//对文件进行重命名 strIconFileName = String.Format(@"{0}Images/{1}/{2}", Server.MapPath("~"), Session["strSiteCode"].ToString(), newName); strIconSaveFileName = String.Format(@"Images/{0}/{1}", Session["strSiteCode"].ToString(), newName); file0.PostedFile.SaveAs(strIconFileName); } } catch (Exception ex) { MessageBox.Show(this, "上传发生错误!原因是:" + ex.ToString()); } ArticleDAL dal = new ArticleDAL(); Model.CMS.CMS_Article modelAdd = new Model.CMS.CMS_Article() { ID = Guid.NewGuid().ToString("N").ToUpper(), //文章标题 Title = this.txtArticleTitle.Text, //图标路径 Pic = strIconSaveFileName,//strIconFileName, //简要说明 Summary = this.txtSummary.Text, //文章内容 Content = this.hd_content.Value, //用户名称 Author = Session["strSiteName"].ToString(), //类别 Category = this.ddlCategory.SelectedValue, //点击次数 ClickNum = 0, //是否置顶 IsTop = this.txtArticleIsTop.Text == "是" ? true : false, //是否删除 IsDel = false, //创建时间 CreateTime = DateTime.Now, //用户登录名 CreateUser = Session["strLoginName"].ToString(), //站点代码 SiteCode = Session["strSiteCode"].ToString() }; if (dal.AddArticleData(modelAdd)) { strMessage = "文章添加成功!"; } else { strMessage = "文章添加失败!"; } MessageBox.Show(this, strMessage); }
/// <summary> /// 单击"保存"按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } //上传图标 string strIconFileName = string.Empty; //图标路径 string strIconSaveFileName = string.Empty; //网址路径 try { if (!string.IsNullOrEmpty(this.file0.PostedFile.FileName)) { if (!System.IO.Directory.Exists(Server.MapPath("~") + @"/Images")) { System.IO.Directory.CreateDirectory(Server.MapPath("~") + @"/Images"); } if (!System.IO.Directory.Exists(String.Format(@"{0}/Images/{1}", Server.MapPath("~"), Session["strSiteCode"].ToString()))) { System.IO.Directory.CreateDirectory(String.Format(@"{0}/Images/{1}", Server.MapPath("~"), Session["strSiteCode"].ToString())); } string orignalName = this.file0.PostedFile.FileName; //获取客户机上传文件的文件名 string extendName = orignalName.Substring(orignalName.LastIndexOf(".")); //获取扩展名 if (extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".png") { MessageBox.Show(this, "文件格式有误!"); return; }//检查文件格式 string newName = String.Format("{0}_{1}{2}", DateTime.Now.Millisecond, file0.PostedFile.ContentLength, extendName);//对文件进行重命名 strIconFileName = String.Format(@"{0}Images/{1}/{2}", Server.MapPath("~"), Session["strSiteCode"].ToString(), newName); strIconSaveFileName = String.Format(@"Images/{0}/{1}", Session["strSiteCode"].ToString(), newName); file0.PostedFile.SaveAs(strIconFileName); } } catch (Exception ex) { MessageBox.Show(this, "上传发生错误!原因是:" + ex.ToString()); } DAL.CMS.ArticleDAL dal = new DAL.CMS.ArticleDAL(); Model.CMS.CMS_Article modelUpdate = new Model.CMS.CMS_Article() { //文章ID ID = strID, //文章标题 Title = this.txtArticleTitle.Text, //图标路径 Pic = strIconSaveFileName, Summary = this.txtSummary.Text, //文章内容 Content = this.hd_content.Value, //文章类别 Category = this.ddlCategory.SelectedValue, //是否置顶 IsTop = (this.ddlArticleIsTop.Text == "是") ? true : false, Order = int.Parse(this.txtOrder.Text.ToString()) }; if (dal.UpdateArticleData(modelUpdate)) { MessageBox.Show(this, "修改成功!"); } else { MessageBox.Show(this, "修改失败!"); } }
/// <summary> /// 添加文章 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool AddArticleData(Model.CMS.CMS_Article model) { StringBuilder strSql = new StringBuilder(); StringBuilder strSql1 = new StringBuilder(); StringBuilder strSql2 = new StringBuilder(); strSql1.Append("ID,"); strSql2.Append(String.Format("'{0}',", model.ID)); if (model.Title != null) { strSql1.Append("Title,"); strSql2.Append(String.Format("'{0}',", model.Title)); } if (model.Pic != null) { strSql1.Append("Pic,"); strSql2.Append(String.Format("'{0}',", model.Pic)); } if (model.Summary != null) { strSql1.Append("Summary,"); strSql2.Append(String.Format("'{0}',", model.Summary)); } if (model.Content != null) { strSql1.Append("Content,"); strSql2.Append(String.Format("'{0}',", model.Content)); } if (model.Author != null) { strSql1.Append("Author,"); strSql2.Append(String.Format("'{0}',", model.Author)); } if (model.Category != null) { strSql1.Append("Category,"); strSql2.Append(String.Format("'{0}',", model.Category)); } strSql1.Append("ClickNum,"); strSql2.Append(model.ClickNum + ","); strSql1.Append("IsTop,"); strSql2.Append((model.IsTop ? 1 : 0) + ","); strSql1.Append("IsDel,"); strSql2.Append((model.IsDel ? 1 : 0) + ","); if (model.CreateTime != null) { strSql1.Append("CreateTime,"); strSql2.Append(String.Format("'{0:yyyy-MM-dd HH:mm:ss}',", model.CreateTime)); } if (model.CreateUser != null) { strSql1.Append("CreateUser,"); strSql2.Append(String.Format("'{0}',", model.CreateUser)); } if (model.SiteCode != null) { strSql1.Append("SiteCode,"); strSql2.Append(String.Format("'{0}',", model.SiteCode)); } strSql.Append("INSERT INTO Cms_Article("); strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1)); strSql.Append(")"); strSql.Append(" VALUES ("); strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1)); strSql.Append(");"); strSql.Append(String.Format(" UPDATE SYS_Account SET ID = '{0}'", model.ID)); strSql.Append(String.Format(" WHERE ID ='{0}' ", model.ID)); int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }
/// <summary> /// 更新文章信息 /// </summary> /// <param name="model">文章信息</param> /// <returns></returns> public bool UpdateArticleData(Model.CMS.CMS_Article model) { StringBuilder strSql = new StringBuilder(); strSql.Append("UPDATE Cms_Article SET "); if (!string.IsNullOrEmpty(model.Title)) { strSql.Append("Title='" + model.Title + "',"); } if (!string.IsNullOrEmpty(model.Pic)) { strSql.Append("Pic='" + model.Pic + "',"); } if (!string.IsNullOrEmpty(model.Summary)) { strSql.Append("Summary='" + model.Summary + "',"); } if (!string.IsNullOrEmpty(model.Content)) { strSql.Append("Content='" + model.Content + "',"); } if (!string.IsNullOrEmpty(model.Author)) { strSql.Append("Author='" + model.Author + "',"); } if (!string.IsNullOrEmpty(model.Category)) { strSql.Append("Category='" + model.Category + "',"); } if (model.ClickNum > 0) { strSql.Append("ClickNum=" + model.ClickNum + ","); } if (!string.IsNullOrEmpty(model.IsTop.ToString())) { strSql.Append("IsTop=" + (model.IsTop ? 1 : 0) + ","); } if (!string.IsNullOrEmpty(model.IsDel.ToString())) { strSql.Append("IsDel=" + (model.IsDel ? 1 : 0) + ","); } if (!string.IsNullOrEmpty(model.CreateUser)) { strSql.Append("CreateUser='******',"); } if (!string.IsNullOrEmpty(model.SiteCode)) { strSql.Append("SiteCode='" + model.SiteCode + "',"); } if (model.Order > 0) { strSql.Append("[Order]=" + model.Order + ","); } int n = strSql.ToString().LastIndexOf(","); strSql.Remove(n, 1); strSql.Append(" WHERE ID ='" + model.ID + "' "); int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }