using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MRSOFTASPNET.XmlBBS;
using MRSOFTASPNET.CommonOperation;
using MRSOFTASPNET.UserCommonOperation;
using System.IO;
public partial class ProjectBBS_AddTitle : System.Web.UI.Page
{
int boardID = -1;
protected void Page_Load(object sender, EventArgs e)
{
///判断用户是否登录
UserInfo info = (UserInfo)UserCommonOperation.GetUserInfo(Session);
if (info == null)
{ ///返回到上一个页面
Response.Write("<script>history.back()</script>");
///跳转到登录页面
Response.Redirect("~/ProjectBBS/UserLogin.aspx");
return;
}
///获取新帖所属的页面ID值,并保存在变量boardID中
if (Request.Params["BoardID"] != null)
{
boardID = DataTypeConvert.ConvertToInt(Request.Params["BoardID"].ToString());
}
if (!Page.IsPostBack)
{
//调用自定义方法BindPageData显示系统中的当前版面的层次信息
BindPageData();
}
///设置按钮的可用性
ListControl[] list = {
ddlBoard,
ddlState
};
ButtonEnable.ControlButtonEnable(btnAdd, list);
ButtonEnable.ControlButtonEnable(btnAddAndReturn, list);
}
public void BindPageData()
{
///显示版面的层次信息
Board board = new Board();
board.CreateHiberarchyBoard(ddlBoard);
if (boardID > 0)
{ ///选择帖子的版面
ListSelectedItem.ListSelectedItemByValue(ddlBoard, boardID.ToString());
}
}
//自定义AddTitle方法将帖子保存到数据库中,并返回帖子的附件的链接地址
private int AddTitle(out string url)
{
//获取用户登录信息
UserInfo info = (UserInfo)UserCommonOperation.GetUserInfo(Session);
if (info == null)
{
url = string.Empty;
return -1;
}
//实例化BBS
BBS bbs = new BBS();
//显示帖子的状态
byte state = (byte)TitleState.Reply;
//调用自定义方法AddAttachmenth上传附件,获取用户上传文件的链接地址
url = AddAttachment(fileUpLoad);
//根据是否上传附件设置帖子的状态
if (string.IsNullOrEmpty(url) == true)
{
//显示用户未上传附件状态
state = byte.Parse(ddlState.SelectedValue);
}
else
{
//显示用户上传附件状态
state = (byte)((int)(TitleState.ReplyAttachment) int.Parse(ddlState.SelectedValue));
}
//添加帖子到数据库中,同时返回新添加帖子的ID值
return (bbs.AddTitle(tbName.Text,
tbBody.Text,
info.UserID,
DataTypeConvert.ConvertToInt(ddlBoard.SelectedValue), state));
}
private string AddAttachment(FileUpload fu)
{
//判断上传文件控件是否存在文件
if(fu.HasFile==false) return null;
//获取上传文件名称
string tfName=fu.PostedFile.FileName;
//创建基于时间的文件名称
string fileName=DealwithString.CreatedStringByTime() tfName.Substring(tfName.LastIndexOf("."));
fileName ="../XmlDatabase/Files/" fileName;
//获取服务器端的文件名称
string allfilePath=Server.MapPath(fileName);
//判断基于服务器端的文件名是否存在,如果存在则不能上传
if(File.Exists(allfilePath) == true)
{
//弹出对话框
Dialog.OpenDialog(Response,
"你上传的文件" fileName "已经存在,不能上传所选择的文件");
}
try
{
//保存上传文件,并返回基于服务器端的文件名称
fu.SaveAs(allfilePath); return (fileName);
}
catch(Exception ex)
{
//导向到错误捕捉页面,并获取当前请求的原始URL及错误信息
Server.Transfer("~/ProjectBBS/ErrorPage.aspx?Url=" Request.RawUrl "& ErrorMsg = " ex.Message,false);
}
return null;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//添加新的帖子
string url = string.Empty;
//调用自定义方法AddTitle方法将帖子保存到数据库中
int titleID = AddTitle(out url);
if (titleID > 0 && string.IsNullOrEmpty(url) == true)
{
//调用Dialog类中的OpenDialog方法弹出对话框
{
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功,但是你没有上传附件……");
this.tbName.Text = this.tbBody.Text = string.Empty;
return;
}
if (titleID > 0)
{
//添加附件信息到数据库中
BBS bbs = new BBS();
if (bbs.AddAttachment(fileUpLoad.FileName,
url,
fileUpLoad.PostedFile.ContentType, titleID) > 0)
{
//弹出添加新帖成功对话框
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功……");
}
else
{
//弹出添加新帖成功,上传附件失败对话框
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功,但是上传附件失败……");
}
}
}
}
protected void btnAddAndReturn_Click(object sender, EventArgs e)
{
///添加新的帖子
string url = string.Empty;
int titleID = AddTitle(out url);
if (titleID > 0 && string.IsNullOrEmpty(url) == true)
{
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功,但是你没有上载附件……");
///返回管理页面
Server.Transfer("~/ProjectBBS/TitleManage.aspx");
return;
}
if (titleID > 0)
{ ///添加附件信息到数据库中
BBS bbs = new BBS();
if (bbs.AddAttachment(fileUpLoad.FileName,
url,
fileUpLoad.PostedFile.ContentType,
titleID) > 0)
{
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功……");
}
else
{
Dialog.OpenDialog(Response, "恭喜您,添加新帖子成功,但是你没有上载附件……");
}
///返回管理页面
Server.Transfer("~/ProjectBBS/TitleManage.aspx");
}
}
protected void btnReturn_Click(object sender, EventArgs e)
{
///返回管理页面
Server.Transfer("~/ProjectBBS/TitleManage.aspx");
}
}
评论