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

📄 addtitle.aspx.cs

📁 精通网络应用系统开发 光盘 该书是人民邮电出版社出版的
💻 CS
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;

public partial class DesktopModules_BBS_AddTitle : System.Web.UI.Page
{
    int nCategoryID = 0;
    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
            TitleM title = new TitleM();
            ContentM content = new ContentM();
            CategoryM category = new CategoryM();
            try
            {   ///添加新帖子
                nTitleID=  title.AddTitle(nCategoryID, Desn.Text.Trim(),
                      Body.Text,
                      Int32.Parse(Session["UserID"].ToString()),
                      nFlag);
                    content.AddContent(nTitleID,Desn.Text.Trim(),
                         Body.Text,
                         Int32.Parse(Session["UserID"].ToString()),
                         nFlag);
                         AddAttachment(nTitleID);
                    category.UpdateCategorySubCount(nCategoryID);
                ///显示操作结果信息
                Response.Write("<script>window.alert('"
                    + ASPNET35System.OPERATIONADDSUCCESSMESSAGE + "')</script>");
            }
            catch (Exception ex)
            {   ///显示操作中的失败、错误信息
                Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
                    + ASPNET35System.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)
    {
        Attachment attch = UploadAttachment();
        if (attch == null)
        {
            return;
        }
        nFlag = 1;

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

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

        Attachment attach = new Attachment();

        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 + -