Sitefinity - Field Widget Control

Posted by Community Admin on 04-Aug-2018 14:55

Sitefinity - Field Widget Control

All Replies

Posted by Community Admin on 08-Dec-2014 00:00

Hi All,

I have created one sitefinity field widget control and registered in my module.  I am populating one dropdown list based on the selection of my field widget control. This functionality is working fine. But when i entered the data in module and trying to save the data at that time custom field control data and its dependent dropdown data is not saving in the database. I have given the code base of my control. Please check and let me know the issue. its very urgent. 

ascx file,

<%@ Control %>
<script type="text/javascript">

    $(document).ready(function ()
        //debugger;
        if ($("#contentViewInsertDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3").length)
            $('#contentViewInsertDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').empty();
       
        else
            $('#contentViewEditDialog_ctl00_ctl00_contentView_productBackendEditView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').empty();
       
        //contentViewEditDialog$ctl00$ctl00$contentView$productBackendEditView$ctl00$ctl00$sections$ctl00$c5953212edc6a748d14Ff0000faaefa$ctl00$ctl00$fields$ctl03$CategoryControl$ctl00$ctl00$ddlCategory
        //contentViewInsertDialog$ctl00$ctl00$contentView$productBackendInsertView$ctl00$ctl00$sections$ctl00$c5953212edc6a748d14Ff0000faaefa$ctl00$ctl00$fields$ctl03$CategoryControl$ctl00$ctl00$ddlCategory
    );
    function CallService()
        //debugger;        
        var ddlCategory = $("#<%=ddlCategory.ClientID%>").val();
        alert('working');
        $.ajax(
            type: "POST",
            url: '/sitefinity/public/services/QuakerOats/ProductService.asmx/GetTypeDetails',
            data: "'CategoryGUID':'" + ddlCategory + "'",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg)
                if ($("#contentViewInsertDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3").length)
                    $('#contentViewInsertDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').html(msg.d);
               
                else
                    $('#contentViewEditDialog_ctl00_ctl00_contentView_productBackendEditView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').html(msg.d);
               
                //$('#contentViewInsertDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').html(msg.d);
                //$('#contentViewEditDialog_ctl00_ctl00_contentView_productBackendInsertView_ctl00_ctl00_sections_c5953212edc6a748d14Ff0000faaefa_0_ctl00_0_ctl00_0_fields_0_typeControl_3_ctl00_3_ctl00_3_dropDown_3').html(msg.d);
            ,
            error: function (e)
                alert('Error');
           
        );
   
</script>

<table>
    <tr>
        <td>
            <asp:Label ID="lblCategory" runat="server" Text="Category" Font-Bold="true"></asp:Label></td>
    </tr>
    <tr>
        <td>
            <asp:DropDownList ID="ddlCategory" runat="server" Width="150px" onchange="CallService();"></asp:DropDownList>
        </td>
    </tr>
</table>
.cs file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.Web.UI.Fields;
using Telerik.Sitefinity.Web.UI.Fields.Contracts;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl

    /// <summary>
    /// A simple field control used to save a string value.
    /// Use the path to this class when you add the field control
    /// SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector
    /// </summary>
    [FieldDefinitionElement(typeof(CategoryTypeSelectorDefinitionElement))]
    public class CategoryTypeSelector : FieldControl
   
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryTypeSelector" /> class.
        /// </summary>
        public CategoryTypeSelector()
       
       
        #endregion

        #region Properties
        //protected override WebControl TitleControl
        //
        //    get
        //    
        //        return this.TitleLabel;
        //    
        //

        //protected override WebControl DescriptionControl
        //
        //    get
        //    
        //        return this.DescriptionLabel;
        //    
        //

        //protected override WebControl ExampleControl
        //
        //    get
        //    
        //        return this.ExampleLabel;
        //    
        //

        /// <summary>
        /// Obsolete. Use LayoutTemplatePath instead.
        /// </summary>
        protected override string LayoutTemplateName
       
            get
           
                return string.Empty;
           
       

        /// <summary>
        /// Gets the layout template's relative or virtual path.
        /// </summary>
        public override string LayoutTemplatePath
       
            get
           
                if (string.IsNullOrEmpty(base.LayoutTemplatePath))
                    return CategoryTypeSelector.layoutTemplatePath;
                return base.LayoutTemplatePath;
           
            set
           
                base.LayoutTemplatePath = value;
           
       

        /// <summary>
        /// Gets the reference to the label control that represents the title of the field control.
        /// </summary>
        /// <remarks>
        /// This control is mandatory only in write mode.
        /// </remarks>
        //protected internal virtual Label TitleLabel
        //
        //    get
        //    
        //        return this.Container.GetControl<Label>("titleLabel", true);
        //    
        //

        /// <summary>
        /// Gets the reference to the label control that represents the description of the field control.
        /// </summary>
        /// <remarks>
        /// This control is mandatory only in write mode.
        /// </remarks>
        //protected internal virtual Label DescriptionLabel
        //
        //    get
        //    
        //        return Container.GetControl<Label>("descriptionLabel", true);
        //    
        //

        /// <summary>
        /// Gets the reference to the label control that displays the example for this
        /// field control.
        /// </summary>
        /// <remarks>
        /// This control is mandatory only in the write mode.
        /// </remarks>
        //protected internal virtual Label ExampleLabel
        //
        //    get
        //    
        //        return this.Container.GetControl<Label>("exampleLabel", true);
        //    
        //

        /// <summary>
        /// Gets the text box control.
        /// </summary>
        /// <value>The text box control.</value>
        //protected virtual TextBox TextBoxControl
        //
        //    get
        //    
        //        return this.Container.GetControl<TextBox>("fieldBox", true);
        //    
        //
        protected internal virtual Label CategoryLabel
       
            get
           
                return this.Container.GetControl<Label>("lblCategory", true);
           
       

        protected virtual DropDownList DdlCategoryControl
       
            get
           
                return this.Container.GetControl<DropDownList>("ddlCategory", true);
           
       
        //[TypeConverter(typeof(ObjectStringConverter))]
        //public override object Value
        //
        //    get
        //    
        //        return this.TextBoxControl.Text;
        //    
        //    set
        //    
        //        this.TextBoxControl.Text = value as string;
        //    
        //
        [TypeConverter(typeof(ObjectStringConverter))]
        public override object Value
       
            get
           
                return this.DdlCategoryControl.SelectedValue;
           
            set
           
                if (value != null)
                    this.DdlCategoryControl.SelectedValue = string.Empty;
           
       
        public string CategoryName get; set;
        #endregion

        #region Methods
        protected override void InitializeControls(GenericContainer container)
       
            //this.TitleLabel.Text = this.Title;
            //this.ExampleLabel.Text = this.Example;
            //this.DescriptionLabel.Text = this.Description;

            //this.TextBoxControl.Text = this.Text;
            BindCategory();

            //Set any existing value, if there is an exception - set the default value
            try
           
                this.DdlCategoryControl.SelectedValue = this.CategoryName;
           
            catch
           
                //set the default value
                this.DdlCategoryControl.SelectedIndex = 1;
           
       
        private void BindCategory()
       
            try
           
                TaxonomyManager manager = TaxonomyManager.GetManager();
                var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Categories").SingleOrDefault();
                var intlRegion = taxonomy.Taxa.Where(t => t.Title == "All Product Category ").SingleOrDefault();
                if (intlRegion != null)
               
                    foreach (HierarchicalTaxon taxon in ((HierarchicalTaxon)intlRegion).Subtaxa.OrderBy(t => t.Title))
                   
                        var li = new ListItem(taxon.Title, Convert.ToString(taxon.Id));
                        DdlCategoryControl.Items.Add(li);
                   
                    DdlCategoryControl.Items.Insert(0, new ListItem("--Select--", "0"));
                    DdlCategoryControl.Items.FindByText("--Select--").Selected = true;
               
           
            catch (Exception exc)
           

           
       
        public override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
       
            List<ScriptDescriptor> descriptors = new List<ScriptDescriptor>();

            if (base.GetScriptDescriptors() != null)
           
                ScriptControlDescriptor descriptor = base.GetScriptDescriptors().Last() as ScriptControlDescriptor;

                if (this.DdlCategoryControl != null)
               
                    //descriptor.AddElementProperty("ddlCategory", this.DdlCategoryControl.ClientID);
                    descriptor.AddElementProperty("ddlCategoryList", this.DdlCategoryControl.ClientID);
                             
                descriptors.Add(descriptor);
           

            return descriptors.ToArray();
       

        public override IEnumerable<ScriptReference> GetScriptReferences()
       
            List<ScriptReference> scripts = new List<ScriptReference>(base.GetScriptReferences());

            scripts.Add(new ScriptReference(CategoryTypeSelector.ScriptReference));

            return scripts;
       

        public override void Configure(IFieldDefinition definition)
       
            base.Configure(definition);

            ICategoryTypeSelectorDefinition fieldDefinition = definition as ICategoryTypeSelectorDefinition;

            if (fieldDefinition != null)
           
                if (!string.IsNullOrEmpty(fieldDefinition.CategoryName))
               
                    this.CategoryName = fieldDefinition.CategoryName;
               
           
       
        #endregion

        #region Private members
        public static readonly string layoutTemplatePath = "~/UserControls/ProductModule/CategoryFieldWidgetControl/CategoryTypeSelector.ascx";
        public static readonly string ScriptReference = "~/UserControls/ProductModule/CategoryFieldWidgetControl/CategoryTypeSelector.js";
        #endregion
   

.js file 

Type.registerNamespace("SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl");

SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector = function (element)
    SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector.initializeBase(this, [element]);
    this._element = element;
    this._labelElement = null;
    //this._textBoxElement = null;
    this._ddlCategoryList = null;


SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector.prototype =
    initialize: function ()
        /* Here you can attach to events or do other initialization */
        SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector.callBaseMethod(this, "initialize");
    ,

    dispose: function ()
        /*  this is the place to unbind/dispose the event handlers created in the initialize method */
        SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector.callBaseMethod(this, "dispose");
    ,

    /* --------------------------------- public methods ---------------------------------- */

    /* --------------------------------- event handlers ---------------------------------- */

    /* --------------------------------- private methods --------------------------------- */

    //_getTextValue: function ()
    //    if (this._textBoxElement)
    //        return this._textBoxElement.value;
    //    
    //    return null;
    //,

    //_clearTextBox: function ()
    //    if (this._textBoxElement != null)
    //        this._textBoxElement.value = "";
    //    
    //,
    _getDropDownValue: function ()
        if (this._ddlCategoryList)            
            return this._ddlCategoryList.value;
       
        return null;
    ,

    /* --------------------------------- properties -------------------------------------- */
    get_value: function ()        
        var val = this._getDropDownValue();
        return val;
    ,
    set_value: function (value)
        if (value !== undefined && value != null && this._ddlCategoryList != null)
            this._ddlCategoryList.value = value;
       
        this._value = value;
    ,


    //get_value: function ()
    //    var val = this._getTextValue();
    //    return val;
    //,

    //set_value: function (value)
    //    this._clearTextBox();
    //    if (value !== undefined && value != null && this._textBoxElement != null)
    //        this._textBoxElement.value = value;
    //    
    //    this._value = value;
    //,

    //get_textBoxElement: function ()
    //    return this._textBoxElement;
    //,

    //set_textBoxElement: function (value)
    //    this._textBoxElement = value;
    //,

    get_ddlCategoryList: function ()
        return this._ddlCategoryList;
    ,

    set_ddlCategoryList: function (value)
        this._ddlCategoryList = value;
     

;

SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector.registerClass("SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl.CategoryTypeSelector", Telerik.Sitefinity.Web.UI.Fields.FieldControl);

definition.cs file 

using System;
using System.Linq;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Web.UI.Fields.Definitions;

namespace SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl

    public class CategoryTypeSelectorDefinition : FieldControlDefinition, ICategoryTypeSelectorDefinition
   
        #region Constuctors
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryTypeSelectorDefinition" /> class.
        /// </summary>
        public CategoryTypeSelectorDefinition()
            : base()
       
       

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryTypeSelectorDefinition" /> class.
        /// </summary>
        /// <param name="configDefinition">The config definition.</param>
        public CategoryTypeSelectorDefinition(ConfigElement configDefinition)
            : base(configDefinition)
       
       
        #endregion

        #region ICategoryTypeSelectorDefinition members
        /// <summary>
        /// Gets or sets the sample text.
        /// </summary>
        public string CategoryName
       
            get
           
                return this.ResolveProperty("CategoryName", this.szCategoryName);
           
            set
           
                this.szCategoryName = value;
           
       
        #endregion

        #region Private members
        private string szCategoryName;
        #endregion
   

DefinitionElement.cs file 

using System;
using System.Configuration;
using System.Linq;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.Web.UI.Fields.Config;

namespace SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl

    public class CategoryTypeSelectorDefinitionElement : FieldControlDefinitionElement, ICategoryTypeSelectorDefinition
   
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryTypeSelectorDefinitionElement" /> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public CategoryTypeSelectorDefinitionElement(ConfigElement parent)
            : base(parent)
       
       
        #endregion

        #region FieldControlDefinitionElement members
        public override DefinitionBase GetDefinition()
       
            return new CategoryTypeSelectorDefinition(this);
       
        #endregion

        #region IFieldDefinition members
        public override Type DefaultFieldType
       
            get
           
                return typeof(CategoryTypeSelector);
           
       
        #endregion

        #region ICategoryTypeSelectorDefinition
        /// <summary>
        /// Gets or sets the sample text.
        /// </summary>
        [ConfigurationProperty("CategoryName")]
        public string CategoryName
       
            get
           
                return (string)this["CategoryName"];
           
            set
           
                this["CategoryName"] = value;
           
       
        #endregion
   


IDefnition.cs file

using System;
using System.Linq;
using Telerik.Sitefinity.Web.UI.Fields.Contracts;

namespace SitefinityWebApp.UserControls.ProductModule.CategoryFieldWidgetControl

    public interface ICategoryTypeSelectorDefinition : IFieldControlDefinition
   
        /// <summary>
        /// Gets or sets the sample text.
        /// </summary>
        /// <value>The sample text.</value>
        string CategoryName get; set;
   



Regards,

Aloy

11 posts
Registered:
12 Jun 2012
07 NOV
LINK TO THIS POST

Hi

I have created Custom Field Controls for blog post With Sitefinity Thunder  and register that control in sitefinity admin. This field is not saving in admin while entering content and click save button.

 

I am attaching three related controls, please inspect  it and let me know if i have done anything wrong. Also please change the file and revert back to me if possible.

Its too urgent, kindly revert

11 posts
Registered:
12 Jun 2012
07 NOV
LINK TO THIS POST

Hi

I have created Custom Field Controls for blog post With Sitefinity Thunder  and register that control in sitefinity admin. This field is not saving in admin while entering content and click save button.

 

I am attaching three related controls, please inspect  it and let me know if i have done anything wrong. Also please change the file and revert back to me if possible.

Its too urgent, kindly revert

Posted by Community Admin on 11-Dec-2014 00:00

Hi Aloy,

Thank you for contacting Sitefinity Support team.

If I can understand it correct you have a dynamic module with category or choice field and you selected a custom field control for editing and displaying?

Can you provide us with some more details on this, please? Moreover, I can see you are calling a custom service with "'CategoryGUID':'" + ddlCategory + "'"  would it be possible for you to explain us what is the logic in there? Is this what should save your changes? Can you describe us the business scenario behind the code you provided with some simple steps. As I can see you bind the dropdown firstly to the “All products Category” taxons.

The rest of the code seems to be fine and nothing incorrect found there. I just need to clarify your scenario and review the code that saves the changes, understand what changes needs to be saved and etc. in order to advice you further on this.

Looking forward hearing from you.

Regards,
Georgi Georgiev
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed