⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addcontent.aspx.cs

📁 一个简单的网上书店形式
💻 CS
字号:
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 ASPNET2BBS;
using System.Data.SqlClient;
using System.IO;

public partial class DesktopModules_BBS_AddContent : System.Web.UI.Page
{
	int nTitleID = 0;
	int nFlag    = 0;

	protected void Page_Load(object sender, EventArgs e)
	{
		///获取参数的值
		if (Request.Params["TitleID"] != null)
		{
			nTitleID = Int32.Parse(Request.Params["TitleID"].ToString());
		}
		if (!IsPostBack)
		{
			ViewState["BackURL"] = Request.UrlReferrer.ToString();
		}
		if (!Page.IsPostBack)
		{
			if (nTitleID > -1)
			{
				BindContentData(nTitleID);
			}
		}
	}

	private void BindContentData(int nTitleID)
	{
		ASPNET2BBS.Title title = new ASPNET2BBS.Title();
		SqlDataReader recc = title.GetSingleTitle(nTitleID);

		if (recc.Read())
		{
			Desn.Text = "Re: " + recc["Desn"].ToString();
			string tempBody = "-------------------------------------------------------\n";
			tempBody += "【" + recc["UserName"].ToString() + "(" +
				recc["AliasName"].ToString() + ")在文章提到:】" + "\n";			
			string tBody = recc["Body"].ToString();
			tempBody += tBody.Length > 50 ? tBody.Substring(0, 50) : tBody;
			tempBody += "\n";			
			Body.Text = tempBody;
		}
		recc.Close();
	}

	protected void AddBtn_Click(object sender, EventArgs e)
	{
		///如果页面输入内容合法
        if (Page.IsValid == true)
        {
            ///定义类User
            ASPNET2BBS.Content content = new ASPNET2BBS.Content();
			ASPNET2BBS.Title title = new Title();
            try
            {
				string sBody = Body.Text;
				int index = sBody.IndexOf("【");
				if(index > -1)
				{
					sBody.Insert(index,"<font color=\"Red\">");
					sBody.Insert(sBody.Length,"</font>");
				}
				
                ///添加新用户
                int nContentID = content.AddContent(nTitleID,Desn.Text.Trim(),
					sBody,
					Int32.Parse(Session["UserID"].ToString()),
					nFlag);
				if (nContentID > -1)
				{					
					AddAttachment(nContentID);
					title.UpdateTitleAnswerNum(nTitleID);
				}

                ///显示操作结果信息
                Response.Write("<script>window.alert('" + ASPNET2System.OPERATIONADDSUCCESSMESSAGE + "')</script>");
            }
            catch (Exception ex)
            {
                ///显示添加操作中的失败、错误信息
                Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
                    + ASPNET2System.RedirectErrorUrl(Request.RawUrl)
                    + "&ErrorMessage=" + ex.Message.Replace("\n", " "));
            }
        }
	}

	protected void ReturnBtn_Click(object sender, EventArgs e)
	{
		Response.Redirect(ViewState["BackURL"].ToString());
	}

	private void AddAttachment(int nContentID)
	{
		AttachmentsInfo attch = UploadAttachment();
		if (attch == null)
		{
			return;
		}
		nFlag = 1;

		///定义类
		Attachment attachment = new Attachment();
		try
		{
			///添加新数据
			attachment.AddAttachment(attch.Desn,
				attch.Url,
				AttachmentFile.PostedFile.ContentType,
				nContentID);
		}
		catch (Exception ex)
		{
			///显示添加操作中的失败、错误信息
			Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
				+ ASPNET2System.RedirectErrorUrl(Request.RawUrl)
				+ "&ErrorMessage=" + ex.Message.Replace("\n", " "));
		}
	}

	private AttachmentsInfo UploadAttachment()
	{
		if (AttachmentFile.PostedFile.ContentLength <= 0)
		{
			return null;
		}

		AttachmentsInfo attach = new AttachmentsInfo();		

		String fileName = AttachmentFile.PostedFile.FileName.Substring(AttachmentFile.PostedFile.FileName.LastIndexOf("\\"),
			AttachmentFile.PostedFile.FileName.Length - AttachmentFile.PostedFile.FileName.LastIndexOf("\\"));
		attach.Desn = fileName;
		String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
			+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
			+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
			+ DateTime.Now.Millisecond.ToString();

		fileName = "\\" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."), fileName.Length - fileName.IndexOf("."));
		string sFileName = Server.MapPath(Request.ApplicationPath) + "\\Attachments" + fileName;

		if (File.Exists(sFileName) == false)
		{
			try
			{
				//把文件存入磁盘,如果失败,导向提示页面
				AttachmentFile.PostedFile.SaveAs(sFileName);
				attach.Url = "\\Attachments" + fileName;
				return (attach);
			}
			catch (Exception ex)
			{
				string sRawURL = Request.RawUrl;

				if (sRawURL.IndexOf("?") > -1)
				{
					sRawURL = sRawURL.Substring(0, sRawURL.IndexOf("?"));
				}
				Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl=" + sRawURL + "&ErrorMessage=" + ex.Message.Replace("\n", " "));
			}
		}
		else
		{
			Response.Write("<script>alert(\"你上传的文件已经存在!\")</script>");
		}

		return (null);
	}

	private String GetRandomint()
	{
		Random random = new Random();
		return (random.Next(10000).ToString());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -