+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / SelectedIndexChanged event for ContentCategoriesField control

SelectedIndexChanged event for ContentCategoriesField control

16 posts, 0 answered
  1. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    02 Jun 2010
    Link to this post
    Hi,

    I want some code to be executed when a user selects a particular Category while adding / editing a news item. But it seems that the ContentCategoriesField Control does not have a SelectedIndexChanged event. How do I achieve this ? I'm using Sitefinity 3.7 Standard Edition SP3.

    Please help.

    Thanks,
    Saumitra
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Jun 2010
    Link to this post
    Hello Saumitra,

    ContentCategoriesField wraps RadTreeView control which you can access from the external template CategoriesView.ascx. Then you can subscribe for the RadTreeView events.

    The template is located under

    [Sitefinity_3_7_standard_ExternalTemplates]\Sitefinity\Admin\ControlTemplates\Generic_Content\CategoriesView.ascx

    You can gather more information about the external templates at How to map a template

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    02 Jun 2010
    Link to this post
    Hi Ivan,

    Actually I wanted to know how do I capture the event raised when a user selects a category from the category drop-down while adding / editing a news item.

    Thanks,
    Saumitra
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Jun 2010
    Link to this post
    Hello Saumitra,

    My post explains exactly how to capture the events. Please read the post again.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    03 Jun 2010
    Link to this post
    Hi Ivan,

    I tried the solution that you'd mentioned. But I'm still not unable to figure out how do I capture the events for ContentCategoriesField dropdown. I'm getting this error in the browser:
    Server Error in '/' Application.
    --------------------------------------------------------------------------------
      
    Unable to cast object of type 'Telerik.Web.UI.RadComboBox' to type 'Telerik.Cms.Engine.WebControls.Admin.IContentCategoriesField'. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
      
    Exception Details: System.InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.RadComboBox' to type 'Telerik.Cms.Engine.WebControls.Admin.IContentCategoriesField'.
      
    Source Error: 
      
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
      
    Stack Trace: 
      
      
    [InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.RadComboBox' to type 'Telerik.Cms.Engine.WebControls.Admin.IContentCategoriesField'.]
       Telerik.Cms.Engine.WebControls.Admin.ContentItemEdit`1.get_CategoriesControl() +53
       Telerik.Cms.Engine.WebControls.Admin.ContentItemEdit`1.SetContentForm(IContent cnt) +2196
       Telerik.Cms.Engine.WebControls.Admin.ContentItemEdit`1.InitializeControls(Control viewContainer) +32
       Telerik.Cms.Engine.WebControls.Admin.ContentItemNew`1.InitializeControls(Control viewContainer) +19
       Telerik.News.WebControls.Admin.NewsItemNew.InitializeControls(Control viewContainer) +18
       Telerik.Cms.Web.UI.ViewModeControl`1.CreateChildControls() +101
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
      
       
      
      
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

    Please let me know what am I missing exactly. Could you please elaborate on the solution that you'd given in your earlier post ?

    Thanks,
    Saumitra
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Jun 2010
    Link to this post
    Hi Saumitra,

    You do not have to cast Telerik.Web.UI.RadComboBox' to'Telerik.Cms.Engine.WebControls.Admin.IContentCategoriesField

    You only have to map the external template CategoriesView.ascx which is used by ContentCategoriesField. In this template there is declaration of RadComboBox control used by ContentCategoriesField. You have to subsciribe for SelectedIndexChanged or another event of the RadComboBox control.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    03 Jun 2010
    Link to this post
    Hi Ivan,

    I'm really sorry but I'm still not able to figure out how would CategoriesView.ascx solve my problem. Maybe I did not explain it properly. Please refer the attached screenshot of exactly what I need. The Category combobox circled in red is the one for which I need to subscribe to the SelectedIndexChanged event.

    Thanks,
    Saumitra
  8. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    04 Jun 2010
    Link to this post
    Hi,

    Any updates on this issue ? Its a bit urgent. Any help would be appreciated.

    Thanks,
    Saumitra
  9. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    07 Jun 2010
    Link to this post
    Hi Team,

    I'm still waiting for your reply on this issue. I've already mentioned earlier that the issue is a bit critical and urgent for me. And i'm still waiting even after more than 72 hours have been passed since my last post.

    Still looking forward to a solution.

    Thanks,
    Saumitra
  10. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    07 Jun 2010
    Link to this post
    Hello Saumitra,

    This dropdown box is actually a control with a control template, so you can edit the control template to achieve what you are looking for. You should first get the external templates for sitefinity. Then create a class in App_Code which inherits from the original control and override the InitializeControls method to subscribe to SelectedIndexChanged event of the RadComboBox holding the categories:
    using System;
    using System.Collections;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Engine.WebControls.Admin;
    using Telerik.Security;
    using Telerik.Web.UI;
    namespace Sitefinity.Samples.WebControls.Admin
    {
        /// <summary>
        /// Summary description for CustomContentCategoriesField
        /// </summary>
        public class CustomContentCategoriesField : ContentCategoriesField
        {
            public override string LayoutTemplatePath
            {
                get
                {
                    object obj = this.ViewState["LayoutTemplatePath"];
                    if (obj != null)
                        return (string)obj;
                    return "~/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx";
                }
                set
                {
                    this.ViewState["LayoutTemplatePath"] = value;
                }
            }
     
            protected override void InitializeControls(System.Web.UI.Control controlContainer)
            {
                this.CategoriesList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(CategoriesList_SelectedIndexChanged);
                base.InitializeControls(controlContainer);
            }
     
            void CategoriesList_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
            {
                //add your logic
            }
        }
    }

    Then go to the control templates where you want to add this custom functionality, for example ~/Sitefinity/Admin/ControlTemplate/News/NewsItemEdit.ascx. Register the control and subsitute the original control with ID category with yours:
    <%@ Register TagPrefix="sfWeb" Namespace="Sitefinity.Samples.WebControls.Admin" Assembly="App_Code" %>
    ...
            <sf:ContentMetaFields ID="MetaFields" runat="server">
                <ItemTemplate>
                            <h3>
                                <asp:Literal ID="Literal15" runat="server" Text="<%$Resources:Category %>"></asp:Literal></h3>
                            <fieldset class="set">
                                <ol class="setIn">
                                    <li class="catSel clearfix">
                                        <asp:Label ID="Label8" AssociatedControlID="Category" runat="server">
                                            <asp:Literal ID="Literal16" runat="server" Text="<%$Resources:Category %>"></asp:Literal>
                                        <em id="Em1" runat="server"></em></asp:Label>
                                        <sfWeb:CustomContentCategoriesField ID="Category" runat="server"></sfWeb:CustomContentCategoriesField>
                                    </li>

    Finally map the control template and restart the website.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  11. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    07 Jun 2010
    Link to this post
    Hi,

    I tried the given solution. But the SelectedIndexChanged event does not get fired.

    Thanks,
    Saumitra
  12. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    07 Jun 2010
    Link to this post
    Hello Saumitra,

    Could you try this code for the custom control:
    using System;
    using System.Collections;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Engine.WebControls.Admin;
    using Telerik.Security;
    using Telerik.Web.UI;
    using System.Web;
    namespace Sitefinity.Samples.WebControls.Admin
    {
        /// <summary>
        /// Summary description for CustomContentCategoriesField
        /// </summary>
        public class CustomContentCategoriesField : ContentCategoriesField
        {
            public override string LayoutTemplatePath
            {
                get
                {
                    object obj = this.ViewState["LayoutTemplatePath"];
                    if (obj != null)
                        return (string)obj;
                    return "~/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx";
                }
                set
                {
                    this.ViewState["LayoutTemplatePath"] = value;
                }
            }
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
            }
     
            protected override void InitializeControls(System.Web.UI.Control controlContainer)
            {
                if (content == null && ContentId != Guid.Empty)
                    content = this.Manager.GetContent(ContentId);
     
                if (content != null)
                {
                    object objSelectedCategory = content.GetMetaData("Category");
                    selectedCategory = objSelectedCategory != null ? objSelectedCategory.ToString() : String.Empty;
                }
     
                this.CategoriesList.SelectedIndexChanged += CategoriesList_SelectedIndexChanged;
                this.CategoriesList.MarkFirstMatch = true;
                this.CategoriesList.AllowCustomText = false;
     
                IList categories = this.Manager.GetCategories(0, 0, "CategoryName");
                RadComboBoxItem liUncategorized = new RadComboBoxItem();
                liUncategorized.Text = "* Uncategorized *";
                liUncategorized.Value = string.Empty;
                if (String.IsNullOrEmpty(selectedCategory))
                    liUncategorized.Selected = true;
                this.CategoriesList.Items.Add(liUncategorized);
     
                AddCategory(Guid.Empty, categories);
            }
     
            private void AddCategory(Guid parentId, IList categories)
            {
                foreach (ICategory category in categories)
                {
                    if (this.CategoriesList.FindItemByValue(category.ID.ToString()) == null && category.ParentCategoryID == parentId)
                    {
                        RadComboBoxItem categoryItem = new RadComboBoxItem(category.CategoryName, category.CategoryName);
                        int level = CalculateLevel(category, categories);
     
                        if (level > 0)
                            categoryItem.Style.Add("padding-left", string.Concat((10 * level), "px"));
                        if (category.CategoryName == selectedCategory)
                            categoryItem.Selected = true;
     
                        this.CategoriesList.Items.Add(categoryItem);
                        AddCategory(category.ID, categories);
                    }
                }
            }
     
            private static int CalculateLevel(ICategory category, IList categories)
            {
                int level = 0;
                while (category.ParentCategoryID != Guid.Empty)
                {
                    level++;
                    foreach (ICategory category1 in categories)
                    {
                        if (category.ParentCategoryID == category1.ID)
                        {
                            category = category1;
                            break;
                        }
                    }
                }
                return level;
            }
     
            private void CategoriesList_SelectedIndexChanged(object sender, EventArgs e)
            {
                RadComboBox list = (RadComboBox)sender;
                if (list.SelectedItem.Value != String.Empty)
                    selectedCategory = list.SelectedItem.Value;
                //Add your logic here
            }
            private IContent content;
            private string selectedCategory;
     
        }
    }


    All the best,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  13. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    09 Jun 2010
    Link to this post
    Hi,

    I implemented the code that you provided. Still no success. Below is the code that I'm trying to execute:

    1) CustomContentCategories Class:
    using System;
    using System.Collections;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Engine.WebControls.Admin;
    using Telerik.Security;
    using Telerik.Web.UI;
      
    namespace PersonalFN.WebControls.Admin
    {
        public delegate void CategoriesEventHandler(object sender, EventArgs e);
      
        public class CustomContentCategoriesField : ContentCategoriesField
        {
            public event CategoriesEventHandler ItemSelected;
      
            public override string LayoutTemplatePath
            {
                get
                {
                    object obj = this.ViewState["LayoutTemplatePath"];
                    if (obj != null)
                        return (string)obj;
                    return "~/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx";
                }
                set
                {
                    this.ViewState["LayoutTemplatePath"] = value;
                }
            }
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
            }
      
            protected override void InitializeControls(System.Web.UI.Control controlContainer)
            {
                if (content == null && ContentId != Guid.Empty)
                    content = this.Manager.GetContent(ContentId);
      
                if (content != null)
                {
                    object objSelectedCategory = content.GetMetaData("Category");
                    selectedCategory = objSelectedCategory != null ? objSelectedCategory.ToString() : String.Empty;
                }
      
                this.CategoriesList.SelectedIndexChanged += CategoriesList_SelectedIndexChanged;
                this.CategoriesList.MarkFirstMatch = true;
                this.CategoriesList.AllowCustomText = false;
      
                IList categories = this.Manager.GetCategories(0, 0, "CategoryName");
                RadComboBoxItem liUncategorized = new RadComboBoxItem();
                liUncategorized.Text = "* Uncategorized *";
                liUncategorized.Value = string.Empty;
                if (String.IsNullOrEmpty(selectedCategory))
                    liUncategorized.Selected = true;
                this.CategoriesList.Items.Add(liUncategorized);
      
                AddCategory(Guid.Empty, categories);
            }
      
            private void AddCategory(Guid parentId, IList categories)
            {
                foreach (ICategory category in categories)
                {
                    if (this.CategoriesList.FindItemByValue(category.ID.ToString()) == null && category.ParentCategoryID == parentId)
                    {
                        RadComboBoxItem categoryItem = new RadComboBoxItem(category.CategoryName, category.CategoryName);
                        int level = CalculateLevel(category, categories);
      
                        if (level > 0)
                            categoryItem.Style.Add("padding-left", string.Concat((10 * level), "px"));
                        if (category.CategoryName == selectedCategory)
                            categoryItem.Selected = true;
      
                        this.CategoriesList.Items.Add(categoryItem);
                        AddCategory(category.ID, categories);
                    }
                }
            }
      
            private static int CalculateLevel(ICategory category, IList categories)
            {
                int level = 0;
                while (category.ParentCategoryID != Guid.Empty)
                {
                    level++;
                    foreach (ICategory category1 in categories)
                    {
                        if (category.ParentCategoryID == category1.ID)
                        {
                            category = category1;
                            break;
                        }
                    }
                }
                return level;
            }
      
            private void CategoriesList_SelectedIndexChanged(object sender, EventArgs e)
            {
                RadComboBox list = (RadComboBox)sender;
                if (list.SelectedItem.Value != String.Empty)
                    selectedCategory = list.SelectedItem.Value;
                if (ItemSelected != null)
                    ItemSelected(sender, e);
            }
      
            private IContent content;
            private string selectedCategory;
        }
    }

    2) The NewsItemNew usercontrol (~/Sitefinity/Admin/ControlTemplates/News/NewsItemNew.ascx) :
    <%@ Register TagPrefix="sfWeb" Namespace="PersonalFN.WebControls.Admin" Assembly="App_Code" %>

    <fieldset class="set">
                               <ol class="setIn">
                                   <li class="catSel clearfix">
                                       <asp:Label ID="Label8" AssociatedControlID="Category" runat="server">
                                           <asp:Literal ID="Literal1" runat="server" Text="Category"></asp:Literal>
                                           <em id="Em1" runat="server"></em></asp:Label>
                                       <sfWeb:CustomContentCategoriesField ID="Category" runat="server"></sfWeb:CustomContentCategoriesField>
                                   </li>
                               </ol>
                           </fieldset>

    3) ControlsConfig.xml file (~/App_Data/Configuration/Telerik.Sitefinity.Configuration.ControlsConfig.xml)
    <viewSettings hostType="Telerik.Cms.Engine.WebControls.Admin.ContentCategoriesField" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx" />

    In spite of this, whenever I select any item from the Category dropdown (please refer the screenshot), the SelectedIndexChanged event does not get fired.

    Am I missing something ?

    Thanks,
    Saumitra
  14. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Jun 2010
    Link to this post
    Hello Saumitra,

    You should not map the control template for the Content Categories Field control in controls config. You should map the cotnrol template for the NewsItemNew control.

    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  15. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    09 Jun 2010
    Link to this post
    Hi,

    I figured out what was the problem. I just had to set the AutoPostBack property to "True" for the CategoriesList control as below:

    protected override void InitializeControls(System.Web.UI.Control controlContainer)
           {
               if (content == null && ContentId != Guid.Empty)
                   content = this.Manager.GetContent(ContentId);
               if (content != null)
               {
                   object objSelectedCategory = content.GetMetaData("Category");
                   selectedCategory = objSelectedCategory != null ? objSelectedCategory.ToString() : String.Empty;
               }
               this.CategoriesList.SelectedIndexChanged += CategoriesList_SelectedIndexChanged;
               this.CategoriesList.MarkFirstMatch = true;
               this.CategoriesList.AllowCustomText = false;
               this.CategoriesList.AutoPostBack = true;
               IList categories = this.Manager.GetCategories(0, 0, "CategoryName");
               RadComboBoxItem liUncategorized = new RadComboBoxItem();
               liUncategorized.Text = "* Uncategorized *";
               liUncategorized.Value = string.Empty;
               if (String.IsNullOrEmpty(selectedCategory))
                   liUncategorized.Selected = true;
               this.CategoriesList.Items.Add(liUncategorized);
               AddCategory(Guid.Empty, categories);
           }


    That solved my problem.

    Thanks,
    Saumitra
  16. Aparna
    Aparna avatar
    20 posts
    Registered:
    05 Sep 2012
    23 Jan 2012
    Link to this post
    hi, 
    I have a similar problem.
    can any one explain me the way to fire a dropdown selected changed event in a custom control Properties.(control designer)

    Thanks
    Aparna
Register for webinar
16 posts, 0 answered