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

📄 attachmentmanage.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.Collections.Generic;
using System.IO;

public partial class DesktopModules_Attachment_AttachmentManage : System.Web.UI.Page
{
    int nDocumentID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {   ///获取ID的值
        if (Request.Params["DocumentID"] != null)
        {
            nDocumentID = Int32.Parse(Request.Params["DocumentID"].ToString());
        }
        ///绑定控件的数据
        if (!Page.IsPostBack)
        {
            if (nDocumentID > -1)
            {
                BindDocumentData(nDocumentID); BindAttachmentData(nDocumentID);
            }
        }
        ///设置更新按钮的可用性
        AddBtn.Enabled = UploadBtn.Enabled = nDocumentID <= -1 ? false : true;
    }
    private void BindDocumentData(int nDocumentID)
    {   ///获取数据
        DocumentM document = new DocumentM();
        Document recd = document.GetSingleDocument(nDocumentID);
        ///读取数据
        if (recd!=null) { DocDesn.Text = recd.Desn; }
    }
    private void BindAttachmentData(int nDocumentID)
    {   ///获取数据
        AttachmentM attachment = new AttachmentM();
        IList<Attachment> dataTable=attachment.GetAttachmentByDocument(nDocumentID);
        ///绑定附件列表的数据
        AccessoryList.DataSource = dataTable;
        ///添加列表控件的ID域
        string[] dataKeyNames = new string[1];
        dataKeyNames[0] = "AttachmentID";
        AccessoryList.DataKeyNames = dataKeyNames;
        AccessoryList.DataBind();
    }

    protected void AddBtn_Click(object sender, EventArgs e)
    {
        ///添加附件和图片,并更新文档的附件标志
        UpdateDocumentIsAttachment(1);
    }
    private void UpdateDocumentIsAttachment(int nFlag)
    {   ///定义类
        DocumentM document = new DocumentM();
        try
        {
            document.UpdateDocumentIsAttachment(nDocumentID, nFlag);
            ///显示操作结果信息
            Response.Write("<script>window.alert('"
                + ASPNET35System.OPERATIONUPDATESUCCESSMESSAGE + "')</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)
    {

    }
    protected void UploadBtn_Click(object sender, EventArgs e)
    {
        ///如果页面输入内容合法
        if (Page.IsValid == true)
        {   ///定义类
            AttachmentM attachment = new AttachmentM();
            try
            {   ///添加新数据
                attachment.AddAttachment(Desn.Text.Trim(), UploadAttachment(),
                    AttachmentFile.PostedFile.ContentType,
                    Int32.Parse(AttachTypeList.SelectedValue), nDocumentID);
                ///绑定附件列表的数据
                BindAttachmentData(nDocumentID);
                ///显示操作结果信息
                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", " "));
            }
        }

    }
    private string UploadAttachment()
    {
        if ((Desn.Text.Trim() != "")
            && (AttachmentFile.PostedFile.ContentLength > 0))
        {   ///获取文件名称
            String fileName = AttachmentFile.PostedFile.FileName.Substring(
                AttachmentFile.PostedFile.FileName.LastIndexOf("\\"),
                AttachmentFile.PostedFile.FileName.Length
                    - AttachmentFile.PostedFile.FileName.LastIndexOf("\\"));
            ///创建时间序列
            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);
                    return ("\\Attachments" + fileName);
                }
                catch (Exception ex){}
            }
            else
            {
                Response.Write("<script>alert(\"         你上传的文件已经存在!\")</script>");
            }
        }
        else
        {
            Response.Write("<script>alert(\"你输入的文件描述\\文件名为空,请重新输入!            \")</script>");
        }
        return ("");
    }
    private String GetRandomint()
    {
        Random random = new Random();
        return (random.Next(10000).ToString());
    }
    protected void AccessoryList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ImageButton deleteBtn = (ImageButton)e.Row.FindControl("deleteBtn");
        if (deleteBtn != null)
        {
            deleteBtn.Attributes.Add("onclick",
                "return confirm('你确定要删除所选择的新闻附件资源吗?');");
        }

    }
    protected void AccessoryList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        return;
    }
    protected void AccessoryList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "delete")
        {   ///定义类
            AttachmentM attachment = new AttachmentM();
            try
            {
                String sUrl = "";
                Attachment reca = attachment.GetSingleAttachment(
                    Int32.Parse(e.CommandArgument.ToString()));
                if (reca!=null){sUrl = reca.Url;}
                ///删除图片或附件
                attachment.DeleteAttachment(Int32.Parse(
                    e.CommandArgument.ToString()));
                if (File.Exists(Server.MapPath(Request.ApplicationPath) + sUrl)
                    == true)
                {
                    File.Delete(Server.MapPath(Request.ApplicationPath) + sUrl);
                }
            }
            catch (Exception ex){}
            ////重新绑定数据
            BindAttachmentData(nDocumentID);
            if (AccessoryList.Rows.Count <= 0){UpdateDocumentIsAttachment(0);}
        }
    }
}

⌨️ 快捷键说明

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