public void GetControl(String controlName)
    {
        ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("MainContent");

        if (cph.HasControls())
        {
            foreach (Control ctrl in cph.Controls)
            {
                if (ctrl.ID == "UpdatePanel1")
                {
                    if (ctrl.HasControls())
                    {
                        foreach (Control ctp in UpdatePanel1.ContentTemplateContainer.Controls)
                        {
                            if (ctp.ID == "txtHistStart")
                            {
                            }
                        }
                    }
                }
            }
        }
    }
Пример #2
0
        protected override void OnInit(EventArgs e)
        {
            FormMode formMode = FormMode.Container;
            PageMode pageMode = PageMode.Render;

            // local variable
            Webpage webpage = this.Webpage;

            if ((Request.QueryString["pagemode"] + "").ToLower() == "edit")
            {
                pageMode = PageMode.Edit;
            }
            //else if (Request.QueryString["pagemode"] + "" == "newpage")
            //	pageMode = PageMode.NewPage;


            bool userHasPermissions = GetUserPermissions();

            if (!userHasPermissions)
            {
                pageMode = PageMode.Render;
            }


            // get and clear place holders
            ContentPlaceHolders = FindControls <ContentPlaceHolder>(this.Body);

            if (ContentPlaceHolders.Count == 0)
            {
                throw new Exception("The masterpage '{0}' has no <asp:Content runat=\"Server\" /> controls");
            }

            ContentPlaceHolders.ForEach(p => p.Controls.Clear());

            // update meta data
            this.Title           = webpage.Title + ((!webpage.IsSiteRoot) ? PurpleSettings.PageSuffix : string.Empty);
            this.MetaDescription = webpage.MetaDescription;
            this.MetaKeywords    = webpage.MetaKeywords;

            if (userHasPermissions)
            {
                HtmlLink adminStylesLink = new HtmlLink()
                {
                    Href = PurpleSettings.CmsPath + "assets/admincontrols/admin.css"
                };
                adminStylesLink.Attributes.Add("rel", "stylesheet");
                this.Header.Controls.Add(adminStylesLink);
            }


            if (pageMode == PageMode.Render)
            {
                // add the admin controls first so the form is already there
                if (userHasPermissions)
                {
                    AddFormToBody();

                    // add admin controls
                    AdminControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/adminpage.ascx");
                    FindAdminControls();

                    // set master selector
                    PopulateMasterPageList();

                    webpageAddButton.Click += new EventHandler(PurplePageAddButton_Click);

                    // fill in the details
                    webpageFilenamePrefix.Text = (!webpage.IsSiteRoot) ? webpage.Url + "/" : "";

                    // versions
                    webpageVersionsRepeater.DataSource = Webpages.GetWebpageRevision(webpage.WebpageID);
                    webpageVersionsRepeater.DataBind();

                    _form.Controls.Add(AdminControl);
                }

                // add common header to first control if needed
                string commonHeader = webpage.CommonAreaHeader;
                if (string.IsNullOrWhiteSpace(commonHeader) && !webpage.IgnoreParentHeader)
                {
                    // go back until the parent is null or we find a
                    Webpage parent = webpage.Parent;
                    do
                    {
                        if (!string.IsNullOrWhiteSpace(parent.CommonAreaHeader))
                        {
                            commonHeader = parent.CommonAreaHeader;
                            break;
                        }


                        parent = parent.Parent;
                    } while (parent != null && !parent.IsSiteRoot);
                }
                // add it if it exists
                if (!String.IsNullOrWhiteSpace(commonHeader))
                {
                    ContentPlaceHolders[0].Controls.Add(new LiteralControl(commonHeader));
                }


                // go through the webpage areas
                foreach (WebpageArea area in webpage.Areas)
                {
                    // get the right placeholder
                    ContentPlaceHolder placeholder = ContentPlaceHolders.Find(c => c.ID == area.ContentPlaceHolderID);

                    // use default
                    if (placeholder == null)
                    {
                        placeholder = ContentPlaceHolders[0];
                    }


                    // build control/HTML parts
                    string content = area.ContentHtml;

                    // fix extra paragraph tags for controls
                    content = _controlFixRegex.Replace(content, "[control:${1}]");

                    // override with single control
                    if (!String.IsNullOrWhiteSpace(area.ControlName))
                    {
                        content = "[control:" + area.ControlName + "]";
                    }


                    // search for controls, load them and create them
                    int             currentPosition = 0;
                    MatchCollection controlMatches  = _controlRegex.Matches(content);

                    if (controlMatches.Count == 0)
                    {
                        // Plain HTML
                        placeholder.Controls.Add(new LiteralControl(area.ContentHtml));
                    }
                    else
                    {
                        foreach (Match controlMatch in controlMatches)
                        {
                            // Add literal for content before custom tag should it exist.
                            if (controlMatch.Index > currentPosition)
                            {
                                placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition, controlMatch.Index - currentPosition)));
                            }

                            // Now lets add our user control.
                            try
                            {
                                string  all         = controlMatch.Groups[1].Value.Trim();
                                Control usercontrol = null;

                                if (!all.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase))
                                {
                                    int index = all.IndexOf(".ascx", StringComparison.OrdinalIgnoreCase) + 5;
                                    usercontrol = LoadControl("~/cms/content/controls/" + all.Substring(0, index));

                                    string   parameters      = Server.HtmlDecode(all.Substring(index));
                                    Type     type            = usercontrol.GetType();
                                    string[] paramCollection = parameters.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                                    foreach (string param in paramCollection)
                                    {
                                        string name  = param.Split('=')[0].Trim();
                                        string value = param.Split('=')[1].Trim();
                                        System.Reflection.PropertyInfo property = type.GetProperty(name);
                                        property.SetValue(usercontrol, Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture), null);
                                    }
                                }
                                else
                                {
                                    usercontrol = LoadControl("~/cms/content/controls/" + all);
                                }

                                placeholder.Controls.Add(usercontrol);

                                // Now we will update our position.
                                //currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                            }
                            catch (Exception controlLoadException)
                            {
                                // Whoopss, can't load that control so lets output something that tells the developer that theres a problem.
                                placeholder.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + "~/cms/content/controls/" + controlMatch.Groups[1].Value + "<br />" + controlLoadException.ToString()));
                            }

                            currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                        }

                        // final text

                        // Add literal for content before custom tag should it exist.
                        if (content.Length > currentPosition)
                        {
                            placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition)));
                        }



                        // check for form needs
                        bool requiresForm = CheckRequiresForm(placeholder);;
                        if (requiresForm)
                        {
                            if (formMode == FormMode.Container)
                            {
                                // transfer controls to form
                                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                                while (placeholder.HasControls())
                                {
                                    form.Controls.Add(placeholder.Controls[0]);
                                }

                                // add form
                                placeholder.Controls.Add(form);
                            }
                            else if (formMode == FormMode.WholePage)
                            {
                                AddFormToBody();
                            }
                        }
                    }



                    #region old rendercode

                    /*
                     *                  if (!String.IsNullOrWhiteSpace(area.ControlName))
                     *                  {
                     *
                     *                          // find the ASCX file
                     *                          string controlVPath = PurpleSettings.CmsPath + "/content/controls/" + area.ControlName;
                     *                          string controlPath = Server.MapPath(controlVPath);
                     *
                     *                          if (File.Exists(controlPath))
                     *                          {
                     *                                  Control control = LoadControl(controlVPath);
                     *                                  bool requiresForm = CheckRequiresForm( control); ;
                     *                                  if (requiresForm)
                     *                                  {
                     *                                          if (formMode == FormMode.Container)
                     *                                          {
                     *                                                  System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                     *                                                  placeholder.Controls.Add(form);
                     *                                                  form.Controls.Add(control);
                     *                                          }
                     *                                          else if (formMode == FormMode.WholePage)
                     *                                          {
                     *                                                  AddFormToBody();
                     *                                          }
                     *                                  }
                     *                                  else
                     *                                  {
                     *                                          placeholder.Controls.Add(control);
                     *                                  }
                     *                          }
                     *                          else
                     *                          {
                     *                                  placeholder.Controls.Add(new LiteralControl("<p>No control at :" + controlVPath + "</p>"));
                     *                          }
                     *
                     *                  }
                     *                  else
                     *                  {
                     *
                     *
                     *
                     *  // search for controls, load them and create them
                     *  string content = area.ContentHtml;
                     *  int currentPosition = 0;
                     *  MatchCollection controlMatches = _controlRegex.Matches(content);
                     *
                     *  if (controlMatches.Count == 0)
                     *  {
                     *      // old HTML only code
                     *      placeholder.Controls.Add(new LiteralControl(area.ContentHtml));
                     *  }
                     *  else
                     *  {
                     *
                     *      foreach (Match controlMatch in controlMatches)
                     *      {
                     *          // Add literal for content before custom tag should it exist.
                     *          if (controlMatch.Index > currentPosition)
                     *          {
                     *              placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition, controlMatch.Index - currentPosition)));
                     *          }
                     *
                     *          // Now lets add our user control.
                     *          try
                     *          {
                     *              string all = controlMatch.Groups[1].Value.Trim();
                     *              Control usercontrol = null;
                     *
                     *              if (!all.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase))
                     *              {
                     *                  int index = all.IndexOf(".ascx", StringComparison.OrdinalIgnoreCase) + 5;
                     *                  usercontrol = LoadControl("~/cms/content/controls/" + all.Substring(0, index));
                     *
                     *                  string parameters = Server.HtmlDecode(all.Substring(index));
                     *                  Type type = usercontrol.GetType();
                     *                  string[] paramCollection = parameters.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                     *
                     *                  foreach (string param in paramCollection)
                     *                  {
                     *                      string name = param.Split('=')[0].Trim();
                     *                      string value = param.Split('=')[1].Trim();
                     *                      System.Reflection.PropertyInfo property = type.GetProperty(name);
                     *                      property.SetValue(usercontrol, Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture), null);
                     *                  }
                     *              }
                     *              else
                     *              {
                     *                  usercontrol = LoadControl("~/cms/content/controls/" + all);
                     *              }
                     *
                     *              placeholder.Controls.Add(usercontrol);
                     *
                     *              // Now we will update our position.
                     *              //currentPosition = myMatch.Index + myMatch.Groups[0].Length;
                     *          }
                     *          catch (Exception controlLoadException)
                     *          {
                     *              // Whoopss, can't load that control so lets output something that tells the developer that theres a problem.
                     *              placeholder.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + "~/cms/content/controls/" + controlMatch.Groups[1].Value + "<br />" + controlLoadException.ToString()));
                     *          }
                     *
                     *          currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                     *      }
                     *
                     *
                     *      // check for form needs
                     *      bool requiresForm = CheckRequiresForm(placeholder); ;
                     *      if (requiresForm)
                     *      {
                     *          if (formMode == FormMode.Container)
                     *          {
                     *              // transfer controls to form
                     *              System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                     *              while (placeholder.HasControls())
                     *              {
                     *                  form.Controls.Add(placeholder.Controls[0]);
                     *              }
                     *
                     *              // add form
                     *              placeholder.Controls.Add(form);
                     *
                     *          }
                     *          else if (formMode == FormMode.WholePage)
                     *          {
                     *              AddFormToBody();
                     *          }
                     *      }
                     *  }
                     *
                     * }
                     */
                    #endregion
                }
            }
            else if (pageMode == PageMode.Edit)
            {
                // just in case
                AddFormToBody();

                // main edit control
                AdminControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/editpage.ascx");
                _form.Controls.Add(AdminControl);

                // attach all the controls
                FindAdminControls();

                // special cases
                if (webpage.IsSiteRoot)
                {
                    webpageFilename.Enabled = false;
                    webpageUrl.Enabled      = false;
                }


                // main attributes
                webpageFilename.Text        = webpage.Filename;
                webpagePageTitle.Text       = webpage.Title;
                webpageMenuTitle.Text       = webpage.MenuTitle;
                webpageMetaDescription.Text = webpage.MetaDescription;
                webpageMetaKeywords.Text    = webpage.MetaKeywords;
                webpageFilenamePrefix.Text  = (!webpage.IsSiteRoot && !webpage.Parent.IsSiteRoot) ? webpage.Parent.Url + "/" : "";
                webpageUrl.Text             = webpage.Url;
                webpageForceSsl.Checked     = webpage.ForceSsl;
                webpageShowInMenu.Checked   = webpage.ShowInMenu;
                webpageSortOrder.Text       = webpage.SortOrder.ToString();

                webpageCommonAreaHeader.Text      = webpage.CommonAreaHeader;
                webpageContentExpirationDate.Text = webpage.ContentExpirationDate.ToString();
                webpageIgnoreParentHeader.Checked = webpage.IgnoreParentHeader;

                // menu type enum



                // master pages
                PopulateMasterPageList();

                // permissions
                string[] allEditors = System.Web.Security.Roles.GetUsersInRole(PurpleSettings.RoleEditor);
                webpageEditors.DataSource = allEditors;
                webpageEditors.DataBind();

                List <string> pageEditors = webpage.Editors.Split(new char[] { ',' }).ToList <String>();
                foreach (ListItem li in webpageEditors.Items)
                {
                    if (pageEditors.Contains(li.Value))
                    {
                        li.Selected = true;
                    }
                }


                // attach save event
                webpageSaveButton.Click += new EventHandler(PurplePageSaveButton_Click);


                // WEBPAGE AREAS


                // list of control folders
                string        controlBase    = Server.MapPath(PurpleSettings.CmsPath + "content/controls/");
                List <string> controlFolders = new List <string>();
                UtilityMethods.FindFoldersRecursive(controlFolders, controlBase);

                // list of controls
                List <string> availableControls = new List <string>();
                UtilityMethods.FindFilesRecursive(availableControls, "*.ascx", controlBase, true);


                // go through the webpage areas
                foreach (WebpageArea area in webpage.Areas)
                {
                    // get the right placeholder
                    ContentPlaceHolder placeholder = ContentPlaceHolders.Find(c => c.ID == area.ContentPlaceHolderID);

                    // use default
                    if (placeholder == null)
                    {
                        placeholder = ContentPlaceHolders[0];
                    }


                    Control areaControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/editarea.ascx");

                    // grab the html area
                    TextBox htmlContent = areaControl.FindControl("HtmlContent") as TextBox;
                    htmlContent.Text = area.ContentHtml;


                    // fill in the *.ascx controls
                    DropDownList controlName = areaControl.FindControl("ControlName") as DropDownList;
                    controlName.DataSource = availableControls;
                    controlName.DataBind();
                    controlName.Items.Insert(0, new ListItem("-- No Control (use HTML) --", ""));

                    if (!String.IsNullOrWhiteSpace(area.ControlName) && controlName.Items.FindByValue(area.ControlName.Replace(".ascx", "")) != null)
                    {
                        controlName.Items.FindByValue(area.ControlName.Replace(".ascx", "")).Selected = true;
                    }

                    // fill in the upload folders
                    DropDownList controlUploadFolder = areaControl.FindControl("ControlUploadFolder") as DropDownList;
                    controlUploadFolder.DataSource = controlFolders;
                    controlUploadFolder.DataBind();
                    controlUploadFolder.Items.Insert(0, new ListItem("-- Root Folder --", ""));

                    // attach event to the upload button
                    (areaControl.FindControl("ControlUploadButton") as Button).Click += new EventHandler(ControlUploadButton_Click);


                    // add this control
                    placeholder.Controls.Add(areaControl);
                }
            }

            base.OnInit(e);
        }