/// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event. Parents CreateChildControls() logic moved here for processing
        /// data of input renderer.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!string.IsNullOrEmpty(QueryTemplate))
            {
                foreach (var param in HttpContext.Current.Request.Params.AllKeys)
                {
                    if (this.QueryTemplate.Contains("%" + param + "%"))
                    {
                        _hasUrlInput = true;
                        break;
                    }
                }

                foreach (var key in HttpContext.Current.Request.Form.AllKeys)
                {
                    var controlName = string.Empty;
                    if (key.Contains('$'))
                    {
                        controlName = key.Remove(0, key.LastIndexOf('$') + 1);
                    }
                    if (QueryTemplate.Contains("%" + controlName + "%"))
                    {
                        _hasFormInput = true;
                        break;
                    }
                }
            }

            Exception controlException = null;

            if (_hasFormInput || HttpContext.Current.Request.QueryString.ToString().Contains(Math.Abs((PortalContext.Current.ContextNodePath + this.ID).GetHashCode()).ToString()) || _hasUrlInput)
            {
                try
                {
                    this.GetQueryFilter();  // initialize query filter to see if query is invalid for empty search
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    controlException = new InvalidContentQueryException(this.QueryTemplate, innerException: ex);
                }

                var errorPanel = this.FindControlRecursive(EmptyQueryErrorPanelID);
                if (errorPanel != null)
                {
                    errorPanel.Visible = _invalidQuery;
                }
                if (_invalidQuery)
                {
                    return;
                }

                Content rootContent = null;

                try
                {
                    rootContent = GetModel() as Content;
                    if (rootContent != null)
                    {
                        rootContent.ChildrenDefinition.AllChildren = AllChildren;
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    if (controlException == null)
                    {
                        controlException = ex;
                    }
                }

                var model = new ParametricSearchViewModel
                {
                    State            = State,
                    SearchParameters = _searchParams.Select(searchParam => new SearchParameter {
                        Name = searchParam.Key, Value = searchParam.Value
                    }).
                                       ToArray()
                };

                try
                {
                    var childCount = 0;
                    if (rootContent != null)
                    {
                        try
                        {
                            childCount = rootContent.Children.Count();
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                            if (controlException == null)
                            {
                                controlException = ex;
                            }
                        }
                    }

                    try
                    {
                        model.Pager = GetPagerModel(childCount, State, string.Empty);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteException(ex);
                        if (controlException == null)
                        {
                            controlException = ex;
                        }

                        //in case of error, set dummy pager model
                        model.Pager = new PagerModel(0, State, string.Empty);
                    }

                    model.Content = rootContent;

                    if (RenderingMode == RenderMode.Xslt)
                    {
                        XmlModelData = model.ToXPathNavigator();
                    }
                    else if (RenderingMode == RenderMode.Ascx || RenderingMode == RenderMode.Native)
                    {
                        if (CanCache && Cacheable && IsInCache)
                        {
                            return;
                        }

                        var viewPath = RenderingMode == RenderMode.Native
                                           ? "/root/Global/Renderers/ContentCollectionView.ascx"
                                           : Renderer;

                        Control presenter = null;

                        try
                        {
                            presenter = Page.LoadControl(viewPath);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                            if (controlException == null)
                            {
                                controlException = ex;
                            }
                        }

                        if (presenter != null)
                        {
                            var ccView = presenter as ContentCollectionView;
                            if (ccView != null)
                            {
                                ccView.Model = model;
                            }

                            if (rootContent != null)
                            {
                                var itemlist = presenter.FindControl(ContentListID);
                                if (itemlist != null)
                                {
                                    try
                                    {
                                        ContentQueryPresenterPortlet.DataBindingHelper.SetDataSourceAndBind(itemlist,
                                                                                                            rootContent.Children);
                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.WriteException(ex);
                                        if (controlException == null)
                                        {
                                            controlException = ex;
                                        }
                                    }
                                }
                            }

                            var itemPager = presenter.FindControl("ContentListPager");
                            if (itemPager != null)
                            {
                                try
                                {
                                    ContentQueryPresenterPortlet.DataBindingHelper.SetDataSourceAndBind(itemPager,
                                                                                                        model.Pager.PagerActions);
                                }
                                catch (Exception ex)
                                {
                                    Logger.WriteException(ex);
                                    if (controlException == null)
                                    {
                                        controlException = ex;
                                    }
                                }
                            }

                            Controls.Add(presenter);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    if (controlException == null)
                    {
                        controlException = ex;
                    }
                }
            }

            try
            {
                if (controlException != null)
                {
                    BuildErrorMessage(controlException);
                }
            }
            catch (Exception ex)
            {
                var errorText = SNSR.GetString(SNSR.Portlets.ContentCollection.ErrorLoadingContentView, HttpUtility.HtmlEncode(ex.Message));

                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(errorText));
            }

            ChildControlsCreated = true;
        }
示例#2
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event. Parents CreateChildControls() logic moved here for processing
        /// data of input renderer.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            var controIdlList = new List <string>();

            if (!String.IsNullOrEmpty(QueryTemplate))
            {
                foreach (var param in HttpContext.Current.Request.Params.AllKeys)
                {
                    if (this.QueryTemplate.Contains("%" + param + "%"))
                    {
                        _hasUrlInput = true;
                        break;
                    }
                }

                foreach (var key in HttpContext.Current.Request.Form.AllKeys)
                {
                    var controlName = string.Empty;
                    if (key.Contains('$'))
                    {
                        controlName = key.Remove(0, key.LastIndexOf('$') + 1);
                    }
                    if (QueryTemplate.Contains("%" + controlName + "%"))
                    {
                        _hasFormInput = true;
                        break;
                    }
                }
            }

            if (_hasFormInput || HttpContext.Current.Request.QueryString.ToString().Contains(Math.Abs((PortalContext.Current.ContextNode.Path + this.ID).GetHashCode()).ToString()) || _hasUrlInput)
            {
                this.GetQueryFilter();  // initialize query filter to see if query is invalid for empty search
                var errorPanel = this.FindControlRecursive(EmptyQueryErrorPanelID);
                if (errorPanel != null)
                {
                    errorPanel.Visible = _invalidQuery;
                }
                if (_invalidQuery)
                {
                    return;
                }

                Content modelData;
                try
                {
                    modelData = GetModel() as Content;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    Controls.Clear();
                    Controls.Add(new LiteralControl("ContentView error: " + ex.Message));
                    return;
                }

                var rootContent = modelData as Content;
                if (rootContent != null)
                {
                    rootContent.AllChildren = AllChildren;
                }

                var model = new ParametricSearchViewModel()
                {
                    State = State
                };

                var searchParamList = new List <SearchParameter>();

                foreach (var searchParam in _searchParams)
                {
                    searchParamList.Add(new SearchParameter()
                    {
                        Name = searchParam.Key, Value = searchParam.Value
                    });
                }

                model.SearchParameters = searchParamList.ToArray();

                try
                {
                    var childCount = rootContent != null ? rootContent.ChildCount : 0;
                    var pm         = new PagerModel(childCount, State, string.Empty);
                    model.Pager = pm;

                    if (RenderingMode == RenderMode.Xslt)
                    {
                        try
                        {
                            XmlModelData = model.ToXPathNavigator();
                        }
                        catch (Exception ex)
                        {
                            Logger.Write(ex.ToString());
                            Logger.WriteException(ex);
                        }
                    }
                    else if (RenderingMode == RenderMode.Ascx || RenderingMode == RenderMode.Native)
                    {
                        model.Content = rootContent;
                        if (CanCache && Cacheable && IsInCache)
                        {
                            return;
                        }

                        var viewPath = RenderingMode == RenderMode.Native
                                           ? "/root/Global/Renderers/ContentCollectionView.ascx"
                                           : Renderer;

                        var presenter = Page.LoadControl(viewPath);
                        if (presenter is ContentCollectionView)
                        {
                            ((ContentCollectionView)presenter).Model = model;
                        }
                        if (rootContent != null)
                        {
                            var itemlist = presenter.FindControl(ContentListID);
                            if (itemlist != null)
                            {
                                ContentQueryPresenterPortlet.DataBindingHelper.SetDataSourceAndBind(itemlist,
                                                                                                    rootContent.Children);
                            }
                        }

                        var itemPager = presenter.FindControl("ContentListPager");
                        if (itemPager != null)
                        {
                            ContentQueryPresenterPortlet.DataBindingHelper.SetDataSourceAndBind(itemPager,
                                                                                                model.Pager.PagerActions);
                        }

                        Controls.Add(presenter);
                    }
                }
                catch (ParserException ex)
                {
                    var errorText = new LiteralControl {
                        Text = ex.Message
                    };
                    Controls.Add(errorText);
                }
                catch (Exception ex)
                {
                    var errorText = new LiteralControl {
                        Text = ex.ToString()
                    };
                    Controls.Add(errorText);
                }
            }
            ChildControlsCreated = true;
        }