Bug in Dynamic Module override
Hi,
After upgrading to Sitefinity 6.1 I'm having troubles with this class:
public class MediaMasterListView : DynamicContentViewMaster [NullReferenceException: Object reference not set to an instance of an object.] Telerik.Sitefinity.DynamicModules.Web.UI.Frontend.DynamicContentViewMaster.ConfigurePager(Int32 virtualItemCount) +353 Telerik.Sitefinity.DynamicModules.Web.UI.Frontend.DynamicContentViewMaster.InitializeControls(GenericContainer container) +284 Telerik.Sitefinity.Web.UI.SimpleView.CreateChildControls() +87 System.Web.UI.Control.EnsureChildControls() +188 System.Web.UI.Control.PreRenderRecursiveInternal() +72 System.Web.UI.Control.PreRenderRecursiveInternal() +255 System.Web.UI.Control.PreRenderRecursiveInternal() +255 System.Web.UI.Control.PreRenderRecursiveInternal() +255 System.Web.UI.Control.PreRenderRecursiveInternal() +255 System.Web.UI.Control.PreRenderRecursiveInternal() +255 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4047Hello Daniel
There has been a slight change in the code. This part of the method:
this.Pager.VirtualItemCount = virtualItemCount; this.Pager.PageSize = this.MasterViewDefinition.ItemsPerPage.Value; this.Pager.QueryParamKey = this.UrlKeyPrefix; this.Pager.BaseUrl = this.PagerBaseUrl; else this.Pager.Visible = false; this.Pager.VirtualItemCount = virtualItemCount; this.Pager.PageSize = this.MasterViewDefinition.ItemsPerPage.Value; this.Pager.QueryParamKey = this.UrlKeyPrefix; this.Pager.BaseUrl = this.PagerBaseUrl; if (this.Host.SetPaginationUrls.HasValue) this.Pager.SetPaginationUrls = this.Host.SetPaginationUrls.Value; else this.Pager.Visible = false; Hi Jen,
MediaDynamicContentView
public class MediaDynamicContentView : DynamicContentView protected override void InitializeMasterView() string str; if (HasValidRelatedDataConfiguration && RelatedItemsIds != null) MasterViewControl.SourceItemsIds = RelatedItemsIds; DynamicContentViewMaster masterViewControl = new MediaMasterListView(); str = (string.IsNullOrEmpty(MasterViewDefinition.TemplateKey) ? DefaultMasterTemplateKey : MasterViewDefinition.TemplateKey); masterViewControl.TemplateKey = str; masterViewControl.DynamicContentType = DynamicContentType; masterViewControl.MasterViewDefinition = MasterViewDefinition; masterViewControl.UrlEvaluationMode = UrlEvaluationMode; masterViewControl.UrlKeyPrefix = UrlKeyPrefix; Controls.Add(masterViewControl); public class MediaMasterListView : DynamicContentViewMaster public override string LayoutTemplatePath get return _layoutTemplatePath; set base.LayoutTemplatePath = value; public override string TemplateKey get return null; protected override string LayoutTemplateName get return null; protected override void InitializeControls(GenericContainer container) SetSorting(); base.InitializeControls(container); //<summary> // Sets the sorting. //</summary> //<param name="sortField">The sort field.</param> //<param name="sortOrder">The sort order.</param> private void SetSorting() var query = new QueryString(HttpContext.Current.Request.Url); var sortField = query.GetParameter("sortField"); var sortOrder = query.GetParameter("sortOrder"); if (string.IsNullOrEmpty(sortField)) sortField = "publicationdate"; if (string.IsNullOrEmpty(sortOrder)) sortOrder = "descending"; var currentSort = (sortOrder == "ascending") ? RadListViewSortOrder.Ascending : RadListViewSortOrder.Descending; var newSort = (currentSort == RadListViewSortOrder.Ascending) ? RadListViewSortOrder.Descending : RadListViewSortOrder.Ascending; switch (sortField.ToLower()) case "publicationdate": query.Parameters.Clear(); query.Parameters.Add("sortField", sortField.ToLower()); query.Parameters.Add("sortOrder", newSort.ToString().ToLower()); SortByDateTop.NavigateUrl = query.MakeAll(); SortByDateBottom.NavigateUrl = query.MakeAll(); SortDateTop.Attributes.Add("class", string.Format("active-0", sortOrder.ToLower())); SortDateBottom.Attributes.Add("class", string.Format("active-0", sortOrder.ToLower())); query.Parameters.Clear(); query.Parameters.Add("sortField", "title"); query.Parameters.Add("sortOrder", newSort.ToString().ToLower()); SortByTitleTop.NavigateUrl = query.MakeAll(); SortByTitleBottom.NavigateUrl = query.MakeAll(); SortTitleTop.Attributes.Clear(); SortTitleBottom.Attributes.Clear(); break; case "title": query.Parameters.Clear(); query.Parameters.Add("sortField", sortField.ToLower()); query.Parameters.Add("sortOrder", newSort.ToString().ToLower()); SortByTitleTop.NavigateUrl = query.MakeAll(); SortByTitleBottom.NavigateUrl = query.MakeAll(); SortTitleTop.Attributes.Add("class", string.Format("active-0", sortOrder.ToLower())); SortTitleBottom.Attributes.Add("class", string.Format("active-0", sortOrder.ToLower())); query.Parameters.Clear(); query.Parameters.Add("sortField", "date"); query.Parameters.Add("sortOrder", newSort.ToString().ToLower()); SortByDateTop.NavigateUrl = query.MakeAll(); SortByDateBottom.NavigateUrl = query.MakeAll(); SortDateTop.Attributes.Clear(); SortDateBottom.Attributes.Clear(); break; var s = (currentSort == RadListViewSortOrder.Ascending) ? "ASC" : "DESC"; MasterViewDefinition.SortExpression = string.Format("0 1", sortField.ToUpper(), s.ToUpper()); protected override void DynamicContentListView_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e) var data = ((RadListViewDataItem)e.Item).DataItem as DynamicContent; var litSummary = e.Item.FindControl("litSummary") as Literal; litSummary.Text = data.GetValue("Summary").ToString(); string url = DataResolver.Resolve(data, "URL", UrlKeyPrefix, MasterViewDefinition.DetailsPageId.ToString()); var socials = new StringBuilder(); var socialUrl = url.ToAbsoluteUrl(); var socialTitle = data.GetValue("Title").ToString() + " #egz #zeewolde " + data.GetValue<string>("ShortUrl"); var socialDescription = data.GetValue("Summary").ToString(); socials.Append(string.Format("<a id='facebook_like' class='addthis_button_facebook_like' addthis:url='0' addthis:title='1' addthis:description='2' fb:like:layout='box_count'></a>", socialUrl, socialTitle, socialDescription)); socials.Append(string.Format("<a id='twitter_like' class='addthis_button_tweet' tw:count='vertical' addthis:url='0' addthis:title='1' addthis:description='2'></a>", socialUrl, socialTitle, socialDescription)); socials.Append(string.Format("<a class='addthis_counter' addthis:url='0' addthis:title='1' addthis:description='2'></a>", socialUrl, socialTitle, socialDescription)); var litSocials = e.Item.FindControl("litSocials") as Literal; litSocials.Text = socials.ToString(); base.DynamicContentListView_ItemDataBound(sender, e); #region Control references protected virtual Pager PagerBottom get return Container.GetControl<Pager>("pager1", true); protected virtual HyperLink SortByDateTop get return this.Container.GetControl<HyperLink>("lnkSortByDateTop", true); protected virtual HtmlGenericControl SortDateTop get return this.Container.GetControl<HtmlGenericControl>("liSortDateTop", true); protected virtual HyperLink SortByTitleTop get return this.Container.GetControl<HyperLink>("lnkSortByTitleTop", true); protected virtual HtmlGenericControl SortTitleTop get return this.Container.GetControl<HtmlGenericControl>("liSortTitleTop", true); protected virtual HyperLink SortByDateBottom get return this.Container.GetControl<HyperLink>("lnkSortByDateBottom", true); protected virtual HtmlGenericControl SortDateBottom get return this.Container.GetControl<HtmlGenericControl>("liSortDateBottom", true); protected virtual HyperLink SortByTitleBottom get return this.Container.GetControl<HyperLink>("lnkSortByTitleBottom", true); protected virtual HtmlGenericControl SortTitleBottom get return this.Container.GetControl<HtmlGenericControl>("liSortTitleBottom", true); #endregion private const string _layoutTemplatePath = "~/Widgets/Media/Templates/MediaMasterListView.ascx"; this.Pager.BaseUrl = this.PagerBaseUrl;private void ConfigurePager(int virtualItemCount) bool? allowPaging = this.MasterViewDefinition.AllowPaging; if ((!allowPaging.GetValueOrDefault() ? true : !allowPaging.HasValue) || !this.MasterViewDefinition.ItemsPerPage.HasValue || this.MasterViewDefinition.ItemsPerPage.Value <= 0) this.Pager.Visible = false; else this.Pager.VirtualItemCount = virtualItemCount; Pager pager = this.Pager; int? itemsPerPage = this.MasterViewDefinition.ItemsPerPage; pager.PageSize = itemsPerPage.Value; this.Pager.QueryParamKey = this.UrlKeyPrefix; this.Pager.BaseUrl = this.PagerBaseUrl; if (base.Host.SetPaginationUrls.HasValue) Pager value = this.Pager; bool? setPaginationUrls = base.Host.SetPaginationUrls; value.SetPaginationUrls = setPaginationUrls.Value; return; Hi Daniel,
As Jen is Out of Office I am handling your request.
Thank you for the provided code snippets and explanation.
Please note that the problem lies in the fact that the Host is null. You'll have to set the Host where you set the MasterViewControl:
protected DynamicContentViewMaster MasterViewControl get if (this.masterViewControl == null) this.masterViewControl = new DynamicContentViewMaster(this.DynamicManager); this.masterViewControl.Host = this; return this.masterViewControl; set this.masterViewControl = value; Hi Veronica,
Thanks for the follow up.
This indeed fixed the problem.
Thanks!
Daniel
Hello,
I'm glad that I helped.
Please let me know if you have more questions.
Hi, I'm using this approach all the time, and it works just great!
But now I'm trying to do the same with DetailViewControl. Has anyone some insight there? Defining and initializing it the same way as for the MasterViewControl, but i only get this error:
A required control was not found in the template for "~/SfCtrlPresentation/_SFCT_/6d39cce0738702b3d1709f68d61b5d097df48ab7/DynamicContentViewDetail.ascx". The control must be assignable from type "Telerik.Sitefinity.DynamicModules.Web.UI.Frontend.DynamicDetailContainer" and must have ID "detailContainer".
What puzzles me is that SF is trying to load this ascx, since my custom DynamicContentViewDetail has it's LayoutTemplatePath set to my own file.
Any insight?
OC
Hi,
The most probable reason for the error might be if the view is not able to find a DynamicDetailContainer control which has the id "detailContainer":
<sf:DynamicDetailContainer id="detailContainer" runat="server"> <LayoutTemplate> </LayoutTemplate></sf:DynamicDetailContainer>