cnuusw

独乐乐不如众乐乐,分享经验,探索行业发展。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

程序流程:/Admin/Admin/SiteOption/SiteInfo.aspx页面初始化,调用Common类中的Cinfig.cs中对应方法,加载xml文件,修改按钮提交后,调用对应方法保存xml。主要代码如下。

 Config.cs类主体代码:

public class Config
    {
        /// <summary>
        /// 获取指定文件的指定节点值
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="tarGet">节点名</param>
        /// <returns>节点值</returns>
        public static string GetConfig(string fileName, string tarGet)
        {
            XmlDocument xdoc = new XmlDocument();
            string _tmp = String.Empty;
            try
            {
                xdoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/" + fileName));
                XmlElement root = xdoc.DocumentElement;
                XmlNodeList elemList = root.GetElementsByTagName(tarGet);
                _tmp = elemList[0].InnerText;
            }
            catch (System.IO.FileNotFoundException)
            {
                HttpContext.Current.Server.Transfer("~/Error.aspx?error=" + HttpContext.Current.Server.UrlEncode("配置文件" + fileName + "丢失,请检查系统!"));
            }
            catch (XmlException)
            {
                HttpContext.Current.Server.Transfer("~/Error.aspx?error=" + HttpContext.Current.Server.UrlEncode("配置文件" + fileName + "损坏,请联系管理员修复!"));
            }
            catch (Exception ex)
            {
                HttpContext.Current.Server.Transfer("~/Error.aspx?error=" + HttpContext.Current.Server.UrlEncode(ex.Message));
            }
            return _tmp;
        }

        /// <summary>
        /// 设置指定文件的指定节点值
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="tarGet">节点名</param>
        /// <param name="val">节点值</param>
        public static void SetConfig(string fileName, string tarGet, string val)
        {
            XmlDocument xdoc = new XmlDocument();
            try
            {
                xdoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/" + fileName));
                XmlElement root = xdoc.DocumentElement;
                XmlNodeList elemList = root.GetElementsByTagName(tarGet);
                elemList[0].InnerText = val;
            }
            catch (Exception ex)
            {
                HttpContext.Current.Server.Transfer("~/Error.aspx?error=" + HttpContext.Current.Server.UrlEncode(ex.Message));
            }
        }

        /// <summary>
        /// 加载(返回)一个指定名称的XML文件
        /// </summary>
        /// <param name="fileName">XML文件名</param>
        /// <returns>XML文件</returns>
        public static XmlDocument GetXML(string fileName)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/" + fileName));
            return xdoc;
        }

        /// <summary>
        /// 保存一个指定的XML文件到App_Data目录
        /// </summary>
        /// <param name="fileName">XML文件名</param>
        /// <param name="xdoc"></param>
        public static void SaveXML(XmlDocument xdoc,string fileName)
        {
            try
            {
                xdoc.Save(HttpContext.Current.Server.MapPath("~/App_Data/" + fileName));
            }
            catch (Exception ex)
            {
                HttpContext.Current.Server.Transfer("~/Error.aspx?error=" + HttpContext.Current.Server.UrlEncode(ex.Message));
            }
        }
    }

 

/Admin/Admin/SiteOption/SiteInfo.aspx 主体代码:

public partial class SiteInfo : EasyFastCMS.Common.BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadInfo();
}
}

private void LoadInfo()
{
XmlDocument xdoc
= Common.Config.GetXML("SiteInfo.xml");
XmlElement root
= xdoc.DocumentElement;
this.Admin_Admin_SiteOption_SiteInfo_txtSiteName.Text = root.GetElementsByTagName("SiteName")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtSiteTitle.Text = root.GetElementsByTagName("SiteTitle")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtSiteUrl.Text = root.GetElementsByTagName("SiteUrl")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtLogo.Text = root.GetElementsByTagName("Logo")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtBanner.Text = root.GetElementsByTagName("Banner")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtSiteManage.Text = root.GetElementsByTagName("SiteManage")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtManageEmail.Text = root.GetElementsByTagName("ManageEmail")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtCopyRight.Text = root.GetElementsByTagName("CopyRight")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtKeyWrods.Text = root.GetElementsByTagName("KeyWords")[0].InnerText;
this.Admin_Admin_SiteOption_SiteInfo_txtDescription.Text = root.GetElementsByTagName("Description")[0].InnerText;
}

protected void Admin_Admin_SiteOption_SiteInfo_btnSubmit_Click(object sender, EventArgs e)
{
XmlDocument xdoc
= Common.Config.GetXML("SiteInfo.xml");
XmlElement root
= xdoc.DocumentElement;
root.GetElementsByTagName(
"SiteName")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtSiteName.Text;
root.GetElementsByTagName(
"SiteTitle")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtSiteTitle.Text;
root.GetElementsByTagName(
"SiteUrl")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtSiteUrl.Text;
root.GetElementsByTagName(
"Logo")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtLogo.Text;
root.GetElementsByTagName(
"Banner")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtBanner.Text;
root.GetElementsByTagName(
"SiteManage")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtSiteManage.Text;
root.GetElementsByTagName(
"ManageEmail")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtManageEmail.Text;
root.GetElementsByTagName(
"CopyRight")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtCopyRight.Text;
root.GetElementsByTagName(
"KeyWords")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtKeyWrods.Text;
root.GetElementsByTagName(
"Description")[0].InnerText = this.Admin_Admin_SiteOption_SiteInfo_txtDescription.Text;
Common.Config.SaveXML(xdoc,
"SiteInfo.xml");
ScriptManager.RegisterClientScriptBlock(
this, this.GetType(), "", "<script language=\"javascript\">alert(\"修改成功!\");</script>", false);
}
}

 

关键点分析:

  public partial class SiteInfo : EasyFastCMS.Common.BasePage 页面继承的基类由默认的System.Web.UI.Page变成自定义的EasyFastCMS.Common.BasePage 在B/S结构的编程中,在每一个页面我们都要去进行一些初始化的检查工作,例如检测Seesion是否过期,检测是否有对应的权限等。但是,如果再每个页面的Load方法中都去检测,那么将造成大量的冗余代码。给程序的维护和开发带来很大负担。所以在B/S结构中,一般需要编写一个默认的基类。再该基类中进行这些判断。BasePage.cs的主要代码如下:

/// <summary>
/// 页面基类
/// </summary>
public class BasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.Page_Load);

}

protected override void OnLoad(EventArgs e)
{
//if (Session.Count < 4)
//{
// Response.Redirect("~/Error.aspx?msg=" + HttpUtility.UrlEncode("登录过期,请重新登录!"));
//}
base.OnLoad(e);
}

#region 权限检查
/// <summary>
/// 页面访问权限ID。
/// </summary>
public virtual int PermissionID
{
get { return -1; }
}

//public AccountsPrincipal CurrentPrincipal
//{
// get
// {
// if (Context.User.Identity.IsAuthenticated)
// {
// AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
// return user;
// }
// return null;
// }
//}

#endregion

#region 页面事件
private void Page_Load(object sender, System.EventArgs e)
{

//string loginPage = ConfigurationManager.AppSettings.Get("LoginPage");//登录页地址
//if (Context.User.Identity.IsAuthenticated)
//{
// //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
// if(Session["UserName"].ToString()!="Administrator")
// {
// if ((PermissionID != -1) && (!user.HasPermissionID(PermissionID)))
// {
// Response.Clear();
// Response.Write("<script defer>window.alert('您没有权限进入本页!');history.back();</script>");
// Response.End();
// }
// }

//}
//else
//{
// FormsAuthentication.SignOut();
// Session.Clear();
// Session.Abandon();
// Response.Redirect("~/Error.aspx?msg=" + HttpUtility.UrlEncode("登录过期,请重新登录!")");
//}
}
#endregion
}

 

下节我们编写系统设置-网站配置-网站参数配置。

本节代码下载地址:http://bbs.easyfast.cn/Code/thread-4-1-1.aspx

求助:博客园是否可以上传附件?自己维护一个论坛挺累的,呵呵。

 

posted on 2011-08-27 15:45  快乐鸟  阅读(567)  评论(2编辑  收藏  举报