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

📄 addcontent.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_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)
    {
       TitleM title = new TitleM();
        Title recc = title.GetSingleTitle(nTitleID);
        if (recc !=null)
        {
            Desn.Text = "Re: " + recc.Desn;
            string tempBody
                = "--------------------------------------------------\n";
            tempBody += "【" + recc.User.UserName + "("
                + recc.User.AliasName + ")在文章提到:】" + "\n";
            string tBody = recc.Body;
            tempBody += tBody.Length > 50 ? tBody.Substring(0, 50) : tBody;
            tempBody += "\n";
            Body.Text = tempBody;
        }
    }

    protected void AddBtn_Click(object sender, EventArgs e)
    {
        ///如果页面输入内容合法
        if (Page.IsValid == true)
        {   ///定义类Content
            ContentM content = new ContentM();
           TitleM title = new TitleM();
            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('"
                    + 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 nContentID)
    {
        Attachment attch = UploadAttachment();
        if (attch == null)
        {
            return;
        }
        nFlag = 1;

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