void wizard_ButtonClick(object sender, BXPageAsDialogButtonClickEventArgs e)
	{
		try
		{
			if (!BXUser.IsCanOperate(BXRoleOperation.Operations.FileManage))
			{
				Close(GetMessage("ACCESS_DENIED"), BXDialogGoodbyeWindow.LayoutType.Error, -1);
				return;
			}

			if (!IsValid)
				return;

			if (e.ButtonEntry != BXPageAsDialogButtonEntry.Done)
				return;

			if (string.IsNullOrEmpty(_clientFileName))
			{
				ShowError(GetMessage("EmptyFileName"));
				return;
			}

			string initClientFileName = _clientFileName;

			if (_clientFileName.EndsWith("."))
				_clientFileName = _clientFileName.Remove(_clientFileName.Length - 1);

			string fnExt = VirtualPathUtility.GetExtension(_clientFileName);
			if (string.IsNullOrEmpty(fnExt))
			{
				_clientFileName = string.Format("{0}.aspx", _clientFileName);
			}
			else if (string.Compare(fnExt, ".aspx", StringComparison.Ordinal) != 0)
			{
				_clientFileName = string.Format("{0}.aspx", _clientFileName);
			}

			//имя 
			string pageNameExtensionless = _clientFileName.Remove(_clientFileName.Length - 5);

			tbxPageName.Text = pageNameExtensionless;
			_clientVirtualPath = VirtualPathUtility.Combine(VirtualPathUtility.AppendTrailingSlash(_clientVirtualDirPath), _clientFileName);

			if (BXSecureIO.FileExists(_clientVirtualPath))
			{
				ShowError(String.Format("{0}: {1}", GetMessage("FileAlreadyExists"), _clientVirtualPath));
				return;
			}

			string menuTypeId = null;
			MenuTypeInfo info = null;
			if (chbxAddToMenu.Checked && ((info = CheckMenuTypeInfo(out menuTypeId)) == null || menuTypeId == null))
			{
				ShowError(GetMessageRaw("Error.YourAccountDontHasTheRightToModifyMenu"));
				return;
			}

			string content = Bitrix.Services.Text.BXDefaultFiles.BuildAspx(!string.IsNullOrEmpty(tbxTitle.Text) ? Encode(tbxTitle.Text) : GetMessage("NoTitle"), _clientVirtualPath, null);

			Bitrix.DataTypes.BXParamsBag<string> keywordBag = new Bitrix.DataTypes.BXParamsBag<string>();

			foreach (string keywordKey in _keywords.Keys)
			{
				if (!IsKeywordValueChanged(keywordKey))
					continue;

				string keywordValue = string.Empty;
				_keywords[keywordKey].TryGetValue("value", out keywordValue);

				if (string.IsNullOrEmpty(keywordValue))
					continue;

				keywordBag.Add(keywordKey, keywordValue);
			}

			content = BXParser.PersistPageKeywords(content, keywordBag);

			BXSecureIO.FileWriteAllText(_clientVirtualPath, content, Bitrix.Configuration.BXConfigurationUtility.DefaultEncoding);

			if (chbxAddToMenu.Checked)
				AddToMenu(menuTypeId, info, pageNameExtensionless);

			BXUndoPageCreationOperation undoOperation = new BXUndoPageCreationOperation();
			undoOperation.FileVirtualPath = _clientVirtualPath;
			undoOperation.MenuTypeId = menuTypeId;

			BXUndoInfo undo = new BXUndoInfo();
			undo.Operation = undoOperation;
			undo.Save();

			BXDialogGoodbyeWindow goodbye = new BXDialogGoodbyeWindow(string.Format(
				GetMessageRaw("NewPageIsSuccessfullyCreated"), 
				string.Concat(undo.GetClientScript(), " return false;"), 
				"#"), -1, BXDialogGoodbyeWindow.LayoutType.Success);
			BXDialogGoodbyeWindow.SetCurrent(goodbye);

			if (chbxEditAfterSave.Checked)
			{
				BXDialogSettings dlgSetting = new BXDialogSettings();
				dlgSetting.Height = 604;
				dlgSetting.Width = 968;
				dlgSetting.MinHeight = 400;
				dlgSetting.MinWidth = 780;

				SwitchToDialog(string.Format("{0}?path={1}&clientType=WindowManager&forcedRedirection=&noundo=", VirtualPathUtility.ToAbsolute("~/bitrix/dialogs/VisualPageEditor.aspx"), HttpUtility.UrlEncode(_clientVirtualPath)), dlgSetting, GetMessage("NewPageIsSuccessfullyCreatedShowEditDialog"), BXDialogGoodbyeWindow.LayoutType.Success, 2000);				
				return;
			}
			Redirect(BXSite.GetUrlForPath(_clientVirtualPath, null), string.Empty, BXDialogGoodbyeWindow.LayoutType.Success);
			return;
		}
		catch (System.Threading.ThreadAbortException)
		{
			//...игнорируем, вызвано Close();
		}
		catch (Exception ex)
		{
			Close(ex.Message, BXDialogGoodbyeWindow.LayoutType.Error, -1);
		}
	}
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        try
        {
			string userPath = string.Empty;
			this.siteId = Request.QueryString["siteId"];
			if (string.IsNullOrEmpty(this.siteId))
			{
				throw new ArgumentNullException("siteId");
			}

			var site = BXSite.GetById(this.siteId);

			if (BXApplicationHelper.ApplicationType == BXApplicationTechnologyType.Mvc)
			{
				var path = Request.QueryString["path"].Replace(site.UrlVirtualPath.TrimEnd(new char[] { '/' }), string.Empty).Replace("~", string.Empty);
				userPath = string.Format(_defaultPathSite, this.siteId) + path.TrimStart(new char[] { '/' });
			}
			else
			{
				userPath = !string.IsNullOrEmpty(Request.QueryString["path"]) ? Request.QueryString["path"] : "~";
			}

			//string userPath = !string.IsNullOrEmpty(Request["path"]) ? Request["path"] : "~/";
            if (!VirtualPathUtility.IsAppRelative(userPath))
            {
                Close(string.Format(GetMessage("ERROR_FORMATTED_PATH_IS_NOT_APP_RELATIVE"), userPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                return;
            }

            CurPath = userPath;
            mCurDir = VirtualPathUtility.GetDirectory(CurPath);
            mCurFile = VirtualPathUtility.GetFileName(CurPath);

            string userQuery = Request.QueryString["query"];
            if (!string.IsNullOrEmpty(userQuery))
                CurQuery = userQuery;

            if (site == null)
            {
                Close(string.Format("{0} '{1}'!", GetMessage("SITE_IS_NOT_FOUND"), CurPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                return;
            }

            if (BXUser.IsCanOperate(BXRoleOperation.Operations.FileManage))
            {
                CurrentMarkup = BXSecureIO.FileReadAllText(CurPath, BXConfigurationUtility.DefaultEncoding);
            }

            mClientPageTitle = !IsPostBack ? !string.IsNullOrEmpty(CurrentMarkup) ? BXParser.ParsePageTitle(CurrentMarkup) : string.Empty : Request.Form["pageTitle"];
            if (string.IsNullOrEmpty(mClientPageTitle))
                mClientPageTitle = GetMessage("UNTITLED_PAGE");

            _keywords = new Dictionary<string, Dictionary<string, string>>();
            _keywordsTypes = BXPageManager.GetKeywords(site.Id);
            foreach (string key in _keywordsTypes.Keys)
            {
                Dictionary<string, string> paramDic = new Dictionary<string, string>();
                paramDic.Add("caption", _keywordsTypes[key]);
                //paramDic.Add("value", GetMessage(string.Format("DefaultKeyword_{0}", key)));
                _keywords.Add(key, paramDic);
            }


            BXSectionInfo curSectionInfo = BXSectionInfo.GetCumulativeSection(mCurDir);
            foreach (KeyValuePair<string, string> curSectionKeyword in curSectionInfo.Keywords)
            {
                if (!_keywordsTypes.ContainsKey(curSectionKeyword.Key))
                    continue;

                Dictionary<string, string> paramDic = null;
                if (!_keywords.TryGetValue(curSectionKeyword.Key, out paramDic))
                    continue;

                paramDic.Add("defaultValue", curSectionKeyword.Value);
            }

            if (BXUser.IsCanOperate(BXRoleOperation.Operations.FileManage))
            {
                BXParamsBag<string> keywordBag = BXParser.ParsePageKeywords(mCurrentMarkup);
                if (keywordBag != null)
                    foreach (KeyValuePair<string, string> keyword in keywordBag)
                    {
                        if (!_keywordsTypes.ContainsKey(keyword.Key))
                            continue;
                        Dictionary<string, string> paramDic = null;
                        if (!_keywords.TryGetValue(keyword.Key, out paramDic))
                            continue;

                        paramDic.Add("value", keyword.Value);
                        //if (!paramDic.ContainsKey("value"))
                        //    paramDic.Add("value", keyword.Value);
                        //else
                        //    paramDic["value"] = keyword.Value;
                    }
            }
            LoadKeywords();

            Behaviour.Settings.MinWidth = 400;
            Behaviour.Settings.MinHeight = 250;
            Behaviour.Settings.Width = 600;
            Behaviour.Settings.Height = 320;
            Behaviour.Settings.Resizeable = true;

            string query = CurQuery;
            if (!string.IsNullOrEmpty(query))
                query = BXQueryString.RemoveParameter(BXConfigurationUtility.Constants.BackUrl, query);

            string backUrl = !string.IsNullOrEmpty(query) ? string.Concat(CurPath, "?", query) : CurPath;

            DescriptionIconClass = "bx-property-page";
            DescriptionParagraphs.Add(string.Format("{0} <b>{1}</b>", GetMessage("DESCRIPTION_HEADER"), CurPath));
            DescriptionParagraphs.Add(
                string.Format(
                    "<a href=\"{0}?path={1}&selectedTabIndex=1&{2}={3}\" >{4}</a>", 
                    VirtualPathUtility.ToAbsolute("~/bitrix/admin/FileManEdit.aspx"), 
                    HttpUtility.UrlEncode(mCurPath), 
                    BXConfigurationUtility.Constants.BackUrl,
                    HttpUtility.UrlEncode(backUrl),
                    GetMessage("GOTO_CONTROL_PANEL")
                    )
                );

			if (BXApplicationHelper.ApplicationType == BXApplicationTechnologyType.Mvc)
			{
				Behaviour.EnableFormIdReplace = false;
			}
        }
        catch (System.Threading.ThreadAbortException /*exception*/)
        {
            //...игнорируем, вызвано Close();
        }
        catch (Exception exception)
        {
            Close(exception.Message, BXDialogGoodbyeWindow.LayoutType.Error, -1);
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        try
        {
            string userPath = Request.QueryString["path"];

            if (string.IsNullOrEmpty(userPath))
            {
                CurrentPath = CurrentDirPath = "~/";
                //ParentDirPath = string.Empty;
            }
            else
            {
                string appRelUserPath = null;
                try
                {
                    appRelUserPath = VirtualPathUtility.ToAppRelative(userPath);
                }
                catch (Exception /*e*/) { }


                if (!string.IsNullOrEmpty(appRelUserPath))
                {
                    CurrentPath = appRelUserPath;
                    CurrentDirPath = VirtualPathUtility.GetDirectory(appRelUserPath);
                    string tmpDirPath = VirtualPathUtility.RemoveTrailingSlash(CurrentDirPath);
                    if(!string.Equals(tmpDirPath, "~", StringComparison.Ordinal))
                        ParentDirPath = VirtualPathUtility.GetDirectory(tmpDirPath);
                }

                if (string.IsNullOrEmpty(CurrentDirPath))
                {
                    Close(string.Format("{0}: {1}!", GetMessage("COULD_NOT_PARSE_PATH"), userPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                    return;
                }
                if (!System.IO.Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath(CurrentDirPath)))
                {
                    Close(string.Format("{0}: {1}!", GetMessage("COULD_NOT_FIND_FOLDER"), CurrentDirPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                    return;
                }
            }

            string userQuery = Request.QueryString["query"];
            if (!string.IsNullOrEmpty(userQuery))
                CurrentQuery = userQuery;

            //...проверка авторизации на модификацию раздела (просмотр без права на изменение не разрешён).
            if (!IsAuthorized())
            { 
                //...диалог авторизации;
                Close(string.Format("{0}: {1}!", GetMessage("INSUFFICIENT_RIGHTS"), CurrentDirPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                return;
            }

            BXSite site = BXSite.GetCurrentSite(CurrentDirPath, Request.Url.Host);
            if (site == null)
            {
                Close(string.Format("{0} '{1}'!", GetMessage("SITE_IS_NOT_FOUND"), CurrentDirPath), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                return;
            }

            _keywords = new Dictionary<string, Dictionary<string, string>>();
            _keywordsTypes = BXPageManager.GetKeywords(site.Id);
            foreach (string key in _keywordsTypes.Keys)
            {
                Dictionary<string, string> paramDic = new Dictionary<string, string>();
                paramDic.Add("caption", _keywordsTypes[key]);
                _keywords.Add(key, paramDic);
            }

            bool isRoot = string.IsNullOrEmpty(ParentDirPath);
            if (!isRoot)
            {
                BXSectionInfo parSectionInfo = BXSectionInfo.GetCumulativeSection(ParentDirPath);
                foreach (KeyValuePair<string, string> parSectionKeyword in parSectionInfo.Keywords)
                {
                    if (!_keywordsTypes.ContainsKey(parSectionKeyword.Key))
                        continue;

                    Dictionary<string, string> paramDic = null;
                    if (!_keywords.TryGetValue(parSectionKeyword.Key, out paramDic))
                        continue;

                    paramDic.Add("defaultValue", parSectionKeyword.Value);
                }
            }

            BXSectionInfo curSectionInfo = BXSectionInfo.GetSection(CurrentDirPath);
            //ClientFolderTitle = curSectionInfo.Name;

            foreach (KeyValuePair<string, string> curSectionKeyword in curSectionInfo.Keywords)
            {
                if (!_keywordsTypes.ContainsKey(curSectionKeyword.Key))
                    continue;

                Dictionary<string, string> paramDic = null;
                if (!_keywords.TryGetValue(curSectionKeyword.Key, out paramDic))
                    continue;

                paramDic.Add("value", curSectionKeyword.Value);
            }



            LoadKeywords();

            Behaviour.Settings.MinWidth = 400;
            Behaviour.Settings.MinHeight = 250;
            Behaviour.Settings.Width = 600;
            Behaviour.Settings.Height = 320;
            Behaviour.Settings.Resizeable = true;

            string query = CurrentQuery;
            if (!string.IsNullOrEmpty(query))
                query = BXQueryString.RemoveParameter(BXConfigurationUtility.Constants.BackUrl, query);

            string backUrl = !string.IsNullOrEmpty(query) ? string.Concat(CurrentPath, "?", query) : CurrentPath;

            DescriptionIconClass = "bx-property-folder";
            DescriptionParagraphs.Add(string.Format("{0} <b>{1}</b>", GetMessage("DESCRIPTION_HEADER"), CurrentDirPath));
            DescriptionParagraphs.Add(
                string.Format(
                    "<a href=\"{0}?path={1}&{2}={3}\" >{4}</a>", 
                    VirtualPathUtility.ToAbsolute("~/bitrix/admin/FileManFolderSettings.aspx"), 
                    HttpUtility.UrlEncode(CurrentDirPath),
                    BXConfigurationUtility.Constants.BackUrl,
                    !string.IsNullOrEmpty(backUrl) ? HttpUtility.UrlEncode(backUrl) : string.Empty,
                    GetMessage("GOTO_CONTROL_PANEL")
                    )
                );
        }
        catch (System.Threading.ThreadAbortException /*exception*/)
        {
            //...игнорируем, вызвано Close();
        }
        catch (Exception exception)
        {
            Close(exception.Message, BXDialogGoodbyeWindow.LayoutType.Error, -1);
        }
    }
    protected void Behaviour_Save(object sender, EventArgs e)
    {
        try
        {
            if (!BXUser.IsCanOperate(BXRoleOperation.Operations.FileManage))
            {
               Close(GetMessage("INSUFFICIENT_RIGHTS"), BXDialogGoodbyeWindow.LayoutType.Error, -1);
                return;
            }

            string pageMarkup = CurrentMarkup;

            Bitrix.DataTypes.BXParamsBag<string> keywordBag = new Bitrix.DataTypes.BXParamsBag<string>();
            foreach (string keywordKey in _keywords.Keys)
            {
                if (!IsKeywordValueChanged(keywordKey))
                    continue;
                string keywordValue = string.Empty;
                _keywords[keywordKey].TryGetValue("value", out keywordValue);
                keywordBag.Add(keywordKey, keywordValue);
            }

            pageMarkup = BXParser.PersistPageKeywords(pageMarkup, keywordBag);
            pageMarkup = BXParser.PersistPageTitle(pageMarkup, mClientPageTitle);

            string savePath = BXPath.Combine(CurDir, CurFile);
            int i = 0;
            while (BXSecureIO.FileExists(savePath + "." + i))
                i++;
            string tempFile = savePath + "." + i;

            BXSecureIO.DemandWrite(tempFile);
            BXSecureIO.DemandWrite(savePath);
                     
            if (BXSecureIO.FileExists(savePath))
                BXSecureIO.FileMove(savePath, tempFile);

            BXSecureIO.SaveAspx(savePath, pageMarkup, null, BXConfigurationUtility.DefaultEncoding);

            if (BXSecureIO.FileExists(tempFile))
                BXSecureIO.FileDelete(tempFile);
               
            Refresh(GetMessage("OPERATION_COMPLETED_SUCCESSFULLY"), BXDialogGoodbyeWindow.LayoutType.Success, -1);
        }
        catch (System.Threading.ThreadAbortException /*exception*/)
        {
            //...на Reload()->HttpResponse.End();
        }
        catch (Exception exception)
        {
            ShowError(exception.Message);
        }
    }