Ejemplo n.º 1
0
        public static bool Delete(ISession session, int templateID)
        {
            ExcelTemplate t = ExcelTemplate.Retrieve(session, templateID);

            if (t == null)
            {
                return(false);
            }
            return(t.Delete(session));
        }
    protected void MagicItemCommand(object sender, MagicItemEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            bool saved = false;
            using (ISession session = new Session())
            {
                ExcelTemplate template;
                if (this.IsAddNew())
                {
                    if (!this.FileUpload1.HasFile)
                    {
                        WebUtil.ShowError(this, "请选择模板文件");
                        return;
                    }
                    if (!this.FileUpload1.FileName.EndsWith(".xls"))
                    {
                        WebUtil.ShowError(this, "请选择有效的Excel文件");
                        return;
                    }
                    string fileName = DateTime.Now.ToString("SN_yyMMdd_HHmmss") + ".xls";
                    string virtualPath = "/Template/" + fileName;
                    this.FileUpload1.SaveAs(Server.MapPath(virtualPath));
                    template = new ExcelTemplate();
                    template.TemplateName = this.txtName.Text.Trim();
                    template.Status = ExcelTemplateStatus.Enable;
                    template.FileVirtualPath = virtualPath;
                    template.Create(session);
                    saved = true;
                }
                else
                {
                    template = ExcelTemplate.Retrieve(session, Cast.Int(this.txtId.Value));
                    if (template == null) return;

                    if (this.FileUpload1.HasFile)
                        //保存文件
                        this.FileUpload1.SaveAs(Server.MapPath(template.FileVirtualPath));
                    template.TemplateName = this.txtName.Text.Trim();
                    if (this.drpStatus.SelectedValue == ((int)ExcelTemplateStatus.Enable).ToString())
                        template.Status = ExcelTemplateStatus.Enable;
                    else
                        template.Status = ExcelTemplateStatus.Disable;
                    template.Update(session, "TemplateName", "Status");
                    saved = true;
                }
            }
            if (saved)
                this.Response.Redirect("ExcelTemplateManager.aspx");
        }
    }