7.0 Two dynamic modules, related data, depending dropdowns solution
Hi all
I want to share something Atanas helped me with achieving. I hope this could help someone someday somewhere :-)
Here is the video that shows best what I wanted to achieve: www.marktold.com/screencast/sf_related_data_solution.swf
I wanted two modules
Module 1 would give categories (hauptkategorie) and subcategories (unterkateforie)
Module 2 would use 1:n subcategories
In my module I wanted
Dropdown 1 to display categories (hauptkategorie) and on change
Dropdown 2 would display the corresponding subcategories (unterkategorie)
Dropdown 2 onSelectedIndexChange would pull the corresponding stuff from Module 2.
Here is what seems to work for me :
my ascx part:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="panoramas_gallery.ascx.cs" Inherits="SitefinityWebApp.Controls.panoramas_gallery" %><%@ Register Assembly="Telerik.Sitefinity.ContentModules" Namespace="Telerik.Sitefinity.Modules.News.Web.UI" TagPrefix="sf" %><%@ Register TagPrefix="sf" Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.PublicControls" %><%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" TagPrefix="sf" %><%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.RelatedData" TagPrefix="sf" %><sf:sitefinityupdatepanel id="SUP" runat="server"><ContentTemplate> <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="True" DataTextField="Hauptkategorie" DataValueField="Hauptkategorie" OnDataBound="RadComboBox1_DataBound" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" Skin="MetroTouch" Width="250"> </telerik:RadComboBox> <div> <telerik:RadComboBox ID="RadComboBox2" runat="server" AutoPostBack="True" DataTextField="Unterkategorie" DataValueField="ID" OnDataBound="RadComboBox2_DataBound" OnSelectedIndexChanged="RadComboBox2_SelectedIndexChanged" Skin="MetroTouch" Width="250px"> </telerik:RadComboBox> <br /> <br /> </div> <asp:Label ID="Label1" runat="server" Text="Grid is comming" Visible="False"></asp:Label> <div class="pano_gallery"> <asp:Repeater ID="Repeater1" runat="server" Visible="false"> <ItemTemplate> <div class="sf_2cols_2_50" style="height: 140px;"> <div class="pano_title"> <h3> <a href="http://www.marktold.ch/panoramas/galerie/html5/output/<;%# Eval("HMTL5") %>" target="_blank"> <%# Eval("Title") %>, <%# Eval("Ort") %></h3> </a> </div> <div class="pano_tech"> <a href="http://www.marktold.ch/panoramas/galerie/html5/output/<;%# Eval("HMTL5") %>" target="_blank"> Panorama laden</a> </div> </div> <div class="sf_2cols_1_50" style="height: 140px;"> <asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Container.DataItem.GetRelatedItems("PanoramaThumb") %>'> <ItemTemplate> <img height="125" src="<%# Eval("ThumbnailUrl")%>" alt="<%# Eval("AlternativeText")%>" id="<%# Eval("Id") %>" title="<%# Eval("Title") %>"> </ItemTemplate> </asp:Repeater> </div> </ItemTemplate> </asp:Repeater> </div> </ContentTemplate></sf:sitefinityupdatepanel>And the codebehind (sorry -> not cleaned up yet)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Telerik.Sitefinity;using Telerik.Sitefinity.Model;using Telerik.Sitefinity.DynamicModules;using Telerik.Sitefinity.Data.Linq.Dynamic;using Telerik.Sitefinity.DynamicModules.Model;using Telerik.Sitefinity.GenericContent.Model;using Telerik.Sitefinity.Modules.Libraries;using Telerik.Sitefinity.Utilities.TypeConverters;using Telerik.Sitefinity.Security;using Telerik.Sitefinity.Lifecycle;using System.Globalization;using System.Threading;using Telerik.Web.UI;using Telerik.OpenAccess;using Telerik.Sitefinity.RelatedData;using System.Collections;using Telerik.Sitefinity.Model.ContentLinks;using Telerik.Sitefinity.DynamicTypes;using Telerik.Sitefinity.RecurrentRules;using Telerik.Sitefinity.Libraries.Model;using Telerik.Sitefinity.Utilities.TypeConverters;using Telerik.Sitefinity.RelatedData;using Telerik.Sitefinity.Pages.Model;using Telerik.Sitefinity.Web.UI.ControlDesign;namespace SitefinityWebApp.Controls public partial class panoramas_gallery : System.Web.UI.UserControl protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) RadComboBox2.Visible = false; RadComboBox1.DataSource = RetrieveCollectionOfHauptkategories(); RadComboBox1.DataBind(); public IQueryable<DynamicContent> RetrieveCollectionOfHauptkategories() // Set the provider name for the DynamicModuleManager here. All available providers are listed in // Administration -> Settings -> Advanced -> DynamicModules -> Providers var providerName = String.Empty; DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName); Type hauptkategorieType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PanorambilderNavigation.Hauptkategorie"); // This is how we get the collection of Hauptkategorie items var myCollection = dynamicModuleManager.GetDataItems(hauptkategorieType).Where(item => item.Status == ContentLifecycleStatus.Live).OrderBy(i => i.FieldValue<string>("Hauptkategorie")); // At this point myCollection contains the items from type hauptkategorieType return myCollection; public List<DynamicContent> RetrieveCollectionOfUnterkategories() var hauptkat = RadComboBox1.SelectedValue; // Set the provider name for the DynamicModuleManager here. All available providers are listed in // Administration -> Settings -> Advanced -> DynamicModules -> Providers var providerName = String.Empty; DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName); Type unterkategoireType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PanorambilderNavigation.Unterkategoire"); Type hauptkategorieType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PanorambilderNavigation.Hauptkategorie"); var parentitem = dynamicModuleManager.GetDataItems(hauptkategorieType).Where(item => item.Status == ContentLifecycleStatus.Live && item.UrlName == hauptkat).FirstOrDefault(); // This is how we get the collection of Unterkategorie items var myCollection = dynamicModuleManager.GetChildItemsHierarchy(parentitem).OrderBy(i => i.FieldValue<string>("Unterkategorie")).ToList(); //var myCollection = dynamicModuleManager.GetDataItems(unterkategoireType).Where(item => item.Status == ContentLifecycleStatus.Live && item.SystemParentItem); // At this point myCollection contains the items from type unterkategoireType return myCollection; protected void RadComboBox1_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e) RadComboBox2.Visible = true; RadComboBox2.DataSource = RetrieveCollectionOfUnterkategories(); RadComboBox2.DataBind(); Repeater1.Visible = false; protected void RadComboBox2_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e) var hauptkat = RadComboBox1.SelectedValue; hauptkat = hauptkat.Replace(" ", "-"); var unterkat = RadComboBox2.SelectedValue; unterkat = unterkat.Replace(" ", "-"); var providerName = String.Empty; Type hauptkategorieType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PanorambilderNavigation.Hauptkategorie"); Type unterkategoireType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.PanorambilderNavigation.Unterkategoire"); Type panorama = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Panoramabilder.Panorama"); DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName); var myCollection = dynamicModuleManager.GetDataItems(hauptkategorieType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master && i.UrlName == hauptkat).FirstOrDefault(); DynamicContent item = dynamicModuleManager.GetDataItems(unterkategoireType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Id.ToString() == unterkat && i.SystemParentId == myCollection.Id).FirstOrDefault(); var relatedParents = RelatedDataExtensions.GetRelatedParentItems(item, "Telerik.Sitefinity.DynamicTypes.Model.Panoramabilder.Panorama").ToList(); Repeater1.Visible = true; Repeater1.DataSource = relatedParents; Repeater1.DataBind(); protected void RadComboBox1_DataBound(object sender, EventArgs e) RadComboBox1.Items.Insert(0, new RadComboBoxItem("Wählen Sie ein Thema", string.Empty)); protected void RadComboBox2_DataBound(object sender, EventArgs e) RadComboBox2.Items.Insert(0, new RadComboBoxItem("Wählen Sie ein Unterthema", string.Empty)); protected string GetFlashUrl(object item) var FlashURL = string.Empty; var FlashFile = item.GetRelatedItems<MediaContent>("Flash").FirstOrDefault(); if (FlashFile != null) FlashURL = FlashFile.Url.ToString(); return FlashURL; protected string GetQtUrl(object item) var QtURL = string.Empty; var QtFile = item.GetRelatedItems<MediaContent>("QuicktimeFile").FirstOrDefault(); if (QtFile != null) QtURL = QtFile.AutoGenerateUniqueUrl.ToString(); return QtURL; protected string GetJPGUrl(object item) var JpgURL = string.Empty; var JpgFile = item.GetRelatedItems<MediaContent>("JPG").FirstOrDefault(); if (JpgFile != null) JpgURL =JpgFile.ItemDefaultUrl.ToString(); return JpgURL; Markus
Hello Markus,
Thank you for sharing the solution with the community. As a token of appreciation I have increased your Telerik points accordingly.
Regards,
Pavel Benov
Telerik