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

📄 addtitle.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_AddTitle : System.Web.UI.Page
{
	int nCategoryID = -1;
	int nFlag = 0;
	int nTitleID = -1;

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

	protected void AddBtn_Click(object sender, EventArgs e)
	{
		///如果页面输入内容合法
		if (Page.IsValid == true)
		{
			///定义类User
			ASPNET2BBS.Title title = new ASPNET2BBS.Title();
			ASPNET2BBS.Content content = new ASPNET2BBS.Content();
			ASPNET2BBS.Category category = new Category();
			try
			{
				///添加新用户
				nTitleID = title.AddTitle(nCategoryID, Desn.Text.Trim(),
					Body.Text,
					Int32.Parse(Session["UserID"].ToString()),
					nFlag);
				
				if (nTitleID > -1)
				{
					content.AddContent(nTitleID,Desn.Text.Trim(),
					Body.Text,
					Int32.Parse(Session["UserID"].ToString()),
					nFlag);
					AddAttachment(nTitleID);

					category.UpdateCategorySubCount(nCategoryID);
				}

				///显示操作结果信息
				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 nTitleID)
	{
		AttachmentsInfo attch = UploadAttachment();
		if (attch == null)
		{
			return;
		}
		nFlag = 1;

		///定义类
		Attachment attachment = new Attachment();
		try
		{
			///添加新数据
			attachment.AddAttachment(attch.Desn,
				attch.Url,
				AttachmentFile.PostedFile.ContentType,
				nTitleID);
		}
		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 + -