📄 createincludefile.aspx.cs
字号:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DNNLite.Manage;
using System.IO;
using Ader.TemplateEngine;
public partial class Manage_CreateIncludeFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindClass();
}
}
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}
private void BindClass()
{
IncludeFileClass[] cls = IncludeFileClass.FindAll(IncludeFileClass.Asc("Id"));
grdClasses.DataSource = cls;
grdClasses.DataBind();
}
protected void grdClasses_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
lblCurrentClass.Text = "当前分类:" + grdClasses.Rows[e.NewSelectedIndex].Cells[1].Text;
BindFiles( int.Parse( grdClasses.DataKeys[e.NewSelectedIndex ].Value.ToString() ) );
btnAddFile.Enabled = true;
ViewState["classid"] = grdClasses.DataKeys[e.NewSelectedIndex].Value.ToString();
}
private void BindFiles( int classid)
{
IncludeFile[] fs = IncludeFile.FindAll(IncludeFile.Desc("Id"), new NHibernate.Expression.EqExpression("ClassId", classid));
grdIncFiles.DataSource = fs;
grdIncFiles.DataBind();
}
protected void btnAddFile_Click(object sender, EventArgs e)
{
ViewState["mode"] = "add";
MultiView1.ActiveViewIndex = 1;
BindFileDate(new IncludeFile());
}
#region 绑定文件
private void BindFileDate(IncludeFile f)
{
MultiView2.ActiveViewIndex = 0;
txtContent.Text = f.Content;
editorl1.HtmlText = f.Content;
txtFileName.Text = f.FileName;
txtphyFile.Text = f.PhyFile;
if (ViewState["mode"] == "add")
{
IncludeFileClass cls = IncludeFileClass.Find(int.Parse(ViewState["classid"].ToString()));
lblOutPutPath.Text = "~/HTML/OutputIncludeFile/" + cls.EName+"/" ;
txtphyFile.Text = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
}
else
{
lblOutPutPath.Text = f.OutDir;
ViewState["id"] = f.Id;
}
rdoTextEdit.Checked = true;
rdoFckEditor.Checked = false;
}
#endregion
protected void rdoTextEdit_CheckedChanged(object sender, EventArgs e)
{
if (rdoTextEdit.Checked)
{
MultiView2.ActiveViewIndex = 0;
}
else
{
MultiView2.ActiveViewIndex = 1;
}
}
protected void rdoFckEditor_CheckedChanged(object sender, EventArgs e)
{
if (rdoTextEdit.Checked)
{
MultiView2.ActiveViewIndex = 0;
}
else
{
MultiView2.ActiveViewIndex = 1;
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
BindFiles( int.Parse(ViewState["classid"].ToString()) );
}
#region "保存"
protected void btnSave_Click(object sender, EventArgs e)
{
IncludeFile f = null;
if (ViewState["mode"] == "add")
{
f = new IncludeFile();
f.CreateTime = DateTime.Now;
f.UpdateTime = DateTime.Now;
}
else
{
f = IncludeFile.Find(int.Parse(ViewState["id"].ToString()));
f.UpdateTime = DateTime.Now;
}
f.ClassId = int.Parse(ViewState["classid"].ToString());
f.FileName = txtFileName.Text;
f.OutDir = lblOutPutPath.Text;
f.PhyFile = txtphyFile.Text;
if (rdoFckEditor.Checked)
{
f.Content = editorl1.HtmlText;
}
else
{
f.Content = txtContent.Text;
}
if (chkAutoSaveLocal.Checked == true)
{
string cnt = editorl1.SaveImgToLocal(f.Content );
editorl1.HtmlText = cnt;
txtContent.Text = cnt;
f.Content = cnt;
}
if (f.IsValid())
{
f.SaveAndFlush();
Response.Write("<script>alert('保存成功')</script>");
}
else
{
lblerr2.Text = f.ValidationErrorMessages[0];
}
}
#endregion
protected void grdIncFiles_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdIncFiles.PageIndex = e.NewPageIndex;
BindFiles( int.Parse(ViewState["classid"].ToString()) );
}
protected void grdIncFiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "E")
{
IncludeFile f = IncludeFile.Find(int.Parse(e.CommandArgument.ToString()));
ViewState["mode"] = "edit";
MultiView1.ActiveViewIndex = 1;
BindFileDate(f);
}
else if (e.CommandName == "C")
{
IncludeFile f = IncludeFile.Find(int.Parse(e.CommandArgument.ToString()));
try
{
CreateFile(f);
Response.Write("<script>alert('已成功生成文件')</script>");
}
catch (Exception ex)
{
lblerr.Text = ex.Message ;
}
}
else if (e.CommandName == "D")
{
IncludeFile.DeleteAll("Id=" + e.CommandArgument.ToString());
BindFiles(int.Parse(ViewState["classid"].ToString()));
}
}
private void CreateFile(IncludeFile f)
{
TemplateManager tm = TemplateManager.FromString(f.Content );
IDictionary<string, object> databag = new Dictionary<string, object>();
DNNLite.Url.Use404StaticUrl.IsCreateStaticUrlAnyWay = true;
DNNLite.Entites.Modules.TempletPortalModuleBase.AddCommBag(ref databag);
foreach (string key in databag.Keys)
{
tm.SetValue(key, databag[key]);
}
string path = Server.MapPath( f.CallPath);
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
using (StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.UTF8))
{
tm.Process(sw);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -