网站内容编辑

EditCategory.aspx

前台

    <form id="form1" runat="server">
    <div>
   <div>
<label for="txtCategoryId">ID:</label><asp:TextBox ID="txtCategoryId" runat="server" />   
    <br />
<label for="txtName">栏目:</label><asp:TextBox ID="txtName" runat="server" MaxLength="20" Width="300" />
<br />
<label for="txtCategory">栏目图片:</label><asp:FileUpload runat="server" ID="ImageUpload" /><asp:Image runat="server" ID="imgCategory" />
<asp:Button runat="server" onclick="btnSa_Click" Text="保存栏目" />
<asp:Button runat="server" OnClick="btnDel_Click" Text="删除栏目" OnClientClick="return confirm('确认删除吗?');" />
    </div>    
    </div>
    </form>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bjxmh.Jingci;
using Anylen.Base.SqlServer;
using System.Data.SqlClient;


public partial class EditCategory : System.Web.UI.Page {


Category _category = new Category();


protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack && Request.Params["id"] != null) {
int id = int.Parse(Request.Params["id"]);
var categoryInfo = _category.GetCategoryInfoById(id);
if (categoryInfo != null) {
txtCategoryId.Text = categoryInfo.Id.ToString();
txtName.Text = categoryInfo.Title;
imgCategory.ImageUrl = categoryInfo.ImageUrl.Trim();


}
}
}


protected void btnSa_Click(object sender, EventArgs e) {


CategoryInfo categoryInfo;
if (string.IsNullOrEmpty(txtCategoryId.Text))
categoryInfo = new CategoryInfo();
else
categoryInfo = _category.GetCategoryInfoById(int.Parse(txtCategoryId.Text));
categoryInfo.Id = Convert.ToInt32(txtCategoryId.Text);
if (ImageUpload.HasFile) {
string filePath = null;
for (int i = 0; i < int.MaxValue; i++) {
filePath = string.Format("/food/images/{0}{1}.jpg", DateTime.Now.ToString("yyMMdd"), i);
if (!System.IO.File.Exists(Server.MapPath(filePath)))
break;
}


if (!string.IsNullOrEmpty(categoryInfo.ImageUrl)) {
System.IO.File.Delete(Server.MapPath(categoryInfo.ImageUrl));
}


ImageUpload.SaveAs(Server.MapPath(filePath));
categoryInfo.ImageUrl = filePath;
}


if (string.IsNullOrEmpty(txtCategoryId.Text)) {
_category.AddCateogoryInfo(categoryInfo);
}
else {
categoryInfo.Id = int.Parse(txtCategoryId.Text);
_category.UpdateCateogoryInfo(categoryInfo);
}


ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('保存成功!');", true);
}


protected void btnDel_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(txtCategoryId.Text)) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('ID为空,无法删除!');", true);
return;
}


_category.DeleteCategoryInfo(int.Parse(txtCategoryId.Text));
ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('删除成功!');", true);
}
}

EditContent.cs

    <form id="form1" runat="server">
    <div>    
<label for="txtId">ID:</label><asp:Literal ID="ltrId" runat="server" />   
    <br />
<label for="txtFullTitle">标题:</label><asp:TextBox ID="txtTitle" runat="server" MaxLength="20" Width="300" />
<br />
<label for="terFullTitle">完整标题:</label><asp:TextBox ID="txtFullTitle" runat="server" MaxLength="50" Width="600" />
<br />
<label for="txtRenqun">人群:</label><asp:TextBox ID="txtRenqun" runat="server"  Width="600" />
<br />
<label for="txtHot">hot:</label><asp:TextBox ID="txtHot" runat="server" MaxLength="8" />
<br />
<label for="txtCategory">栏目:</label><jingci:CategoryDropDownList runat="server" ID="ddlCategory" IsRequired="true" AddEmpty="true" />
<br />
<label for="txtCategory">产品图片:</label><asp:FileUpload runat="server" ID="foodImageUpload" /><asp:Image runat="server" ID="imgFood" />
<br />
<asp:Button runat="server" onclick="btnSave_Click" Text="保存文章" />
<asp:Button runat="server" OnClick="btnDelete_Click" Text="删除文章" OnClientClick="return confirm('确认删除吗?');" />
    </div>
    </form>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bjxmh.Jingci;
using Anylen.Base.SqlServer;
using System.Data.SqlClient;


public partial class EditContent : System.Web.UI.Page {


Food _food = new Food();


protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack && Request.Params["id"] != null) {
int id = int.Parse(Request.Params["id"]);
var foodInfo = _food.GetContentInfoById(id);
if (foodInfo != null) {
ltrId.Text = foodInfo.Id.ToString();
txtTitle.Text = foodInfo.Title;
txtFullTitle.Text = foodInfo.FullTitle;
txtHot.Text = foodInfo.Hot.ToString();
txtRenqun.Text = foodInfo.Renqun;
ddlCategory.Refresh();
ddlCategory.SelectedValue = foodInfo.CategoryId.ToString();
imgFood.ImageUrl = foodInfo.ImageUrl;
}
}
}


protected void btnSave_Click(object sender, EventArgs e) {


FoodInfo foodInfo;
if (string.IsNullOrEmpty(ltrId.Text))
foodInfo = new FoodInfo();
else
foodInfo = _food.GetContentInfoById(int.Parse(ltrId.Text));


foodInfo.Title = txtTitle.Text;
foodInfo.FullTitle = txtFullTitle.Text;
foodInfo.Renqun = txtRenqun.Text;
foodInfo.Hot = Convert.ToInt32(txtHot.Text);
foodInfo.CategoryId = Convert.ToInt32(ddlCategory.SelectedValue);


if (foodImageUpload.HasFile) {
string filePath = null;
for (int i = 0; i < int.MaxValue; i++) {
filePath = string.Format("/tools/food/images/{0}{1}.jpg", DateTime.Now.ToString("yyMMdd"), i);
if (!System.IO.File.Exists(Server.MapPath(filePath)))
break;
}


if (!string.IsNullOrEmpty(foodInfo.ImageUrl)) {
System.IO.File.Delete(Server.MapPath(foodInfo.ImageUrl));
}


foodImageUpload.SaveAs(Server.MapPath(filePath));
foodInfo.ImageUrl = filePath;
}


if (string.IsNullOrEmpty(ltrId.Text)) {
_food.AddContentInfoes(foodInfo);
}
else {
foodInfo.Id = int.Parse(ltrId.Text);
_food.UpdateContentInfo(foodInfo);
}


ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('保存成功!');", true);
}


protected void btnDelete_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(ltrId.Text)) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('ID为空,无法删除!');", true);
return;
}


_food.DeleteContentInfo(int.Parse(ltrId.Text));
ScriptManager.RegisterStartupScript(this, this.GetType(), "information", "alert('删除成功!');", true);
}



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值