using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Administrator_EditPageContent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack==false)
        {
            Initialize();
        }
    }

    private void Initialize()
    {
        
    }

    protected virtual void DisplayErrorMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class='error'>[{0}]</div>",message);
    }

    protected virtual void DisplayWarningMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class='warning'>[{0}]</div>", message);
    }

    protected virtual void DisplayInformationMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class='information'>[{0}]</div>", message);
    }
    protected void btnOpen_Click(object sender, EventArgs e)
    {
        txtFileName.Text =
            txtFileName.Text.Trim();

        if (txtFileName.Text==string.Empty)
        {
            string strErrorMessage =
                "You Did Not Specify File Name For Opening !";
            DisplayErrorMessage(strErrorMessage);
            return;
        }

        string strFileName = txtFileName.Text;
        string strRootRelativePath = "~/App_Data/PageContent";
        string strRootRelativePathName =
            string.Format("{0}/{1}",strRootRelativePath,strFileName);

        string strPathName = Server.MapPath(strRootRelativePathName);

        if (System.IO.File.Exists(strPathName)==false)
        {
            string strErrorMessage =
                string.Format("The File [{0}] Dose Not Exists For Opening !",txtFileName.Text);
            DisplayErrorMessage(strErrorMessage);
            return;
        }

        System.IO.StreamReader oStreamReader = null;

        try
        {
            oStreamReader = new System.IO.StreamReader(strPathName, System.Text.Encoding.UTF8);
            txtEditPage.Text = oStreamReader.ReadToEnd();

            string strInformationMessage =
                string.Format("The File [{0}] Opened Successfully . . .",txtFileName.Text);
            DisplayInformationMessage(strInformationMessage);
        }
        catch (Exception ex)
        {

            DisplayErrorMessage(ex.Message);
        }

        finally
        {
            if (oStreamReader!=null)
            {
                oStreamReader.Dispose();
                oStreamReader=null;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        txtFileName.Text =
            txtFileName.Text.Trim();

        if (txtFileName.Text==string.Empty)
        {
            string strErrorMessage =
                "You Did Not Specify File For Opening!";
            DisplayErrorMessage(strErrorMessage);

        }

        string strFileName = txtFileName.Text;
        string strRootRelativePath = "~/App_Data/PageContent";
        string strRootRelativePathName =
            string.Format("{0}/{1}",strRootRelativePath,strFileName);

        string strPathName=Server.MapPath(strRootRelativePathName);

        System.IO.StreamWriter oStreamWriter = null;

        try
        {
            oStreamWriter = new System.IO.StreamWriter
                (strPathName, false, System.Text.Encoding.UTF8);
            oStreamWriter.Write(txtEditPage.Text);

            string strInformationMessage =
                string.Format("The File [{0}] Saved Saccessfully . . .",txtFileName.Text);
            DisplayInformationMessage(strInformationMessage);
        }
        catch (Exception ex)
        {

            DisplayErrorMessage(ex.Message);
        }

        finally
        {
            if (oStreamWriter!=null)
            {
                oStreamWriter.Dispose();
                oStreamWriter = null;
            }
        }
    }
}