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

Forums / Module Builder / Programmatically setting the DropDownList in value method

Programmatically setting the DropDownList in value method

12 posts, 0 answered
  1. Mohanakrishnakumar
    Mohanakrishnakumar avatar
    6 posts
    Registered:
    14 May 2013
    02 Jul 2013
    Link to this post
    Hello,
    I created two content types (units and courses) within a dynamic module where the courses has a asp DropDownList containing the various units id's as values. When i select a value in the DropDown and publish, it says its published but when i edit the same course, I see that the dropdownlist value is not the one i selected. however, when i reselect the same and publish again and then edit the same course again, the value is what i have selected. Please let me know where I am going wrong.
    I have pasted the code behind file generated using thunder.
    public override object Value
    {
        get
        {
            return this.DropDownListControl.SelectedValue ;
        }
        set
        {
           //this.DropDownListControl.SelectedValue = value as string;
            this.DropDownListControl.SelectedValue = value as string;
        }
    }
     
    public string Text { 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;
     
        // Fetch a collection of "live" and "visible" units items.
        var myCollection = GetDataItems();
     
        // Binds the collection of Person items to the RadGrid
        this.DropDownListControl.DataSource = myCollection;
        this.DropDownListControl.DataTextField = "Title";
        this.DropDownListControl.DataValueField = "id";
        this.DropDownListControl.DataBind();
    }
     
    // Gets a collection of "live" and "visible" units items.
    public IQueryable<DynamicContent> GetDataItems()
    {
        DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
        Type unitsType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.UnitView.Units");
     
        // Fetch a collection of "live" and "visible" units items.
        var myCollection = dynamicModuleManager.GetDataItems(unitsType)
            .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
     
        return myCollection;
    }

    //ascx
    <asp:DropDownList ID="dropDown" runat="server">
        <asp:ListItem Text="Title" Value="id">-Select-</asp:ListItem>
    </asp:DropDownList>
  2. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    05 Jul 2013
    Link to this post
    Hello,

    We have answered you on the support ticket you have opened. You could share the solution with the community once the problem is resolved.

    Regards,
    Stefani Tacheva
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Mateen
    Mateen avatar
    13 posts
    Registered:
    29 Aug 2012
    30 Nov 2013 in reply to Stefani Tacheva
    Link to this post
    Hi Stefani,

    Can you please share the code, how to fill custom data in dropdownlist in Module builder.
    And how to fetch it pragmatically.

    Mateen
  4. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    02 Dec 2013
    Link to this post
    Hi,

    Please review the following blog post. I believe it is covering what you are currently trying to implement and it might be useful to you.

    Regards,
    Stefani Tacheva
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. Mohanakrishnakumar
    Mohanakrishnakumar avatar
    6 posts
    Registered:
    14 May 2013
    02 Dec 2013
    Link to this post
    Hello Mateen,

    I tried something else to make that work. I used a ChoiceField. Please find my code below
    public class HomeUnitSelector : ChoiceField
        {
            /// <summary>
            /// Defaults the rendering of the list as a drop-down menu
            /// </summary>
            /// <value>
            /// The option for which to render choices.
            /// </value>
            public override RenderChoicesAs RenderChoicesAs
            {
                get { return RenderChoicesAs.DropDown; }
                set { base.RenderChoicesAs = value; }
            }
     
            /// <summary>
            /// Defaults the script descriptor to be ChoiceField so we don't have to reimplement the JavaScript for the field.
            /// </summary>
            /// <value>
            /// The type of the script descriptor.
            /// </value>
            protected override string ScriptDescriptorType
            {
                get { return typeof(ChoiceField).FullName; }
            }
     
            /// <summary>
            /// Default the type of the resource assembly to ChoiceField so we don't have to implement a new resource file
            /// </summary>
            protected override Type ResourcesAssemblyInfo
            {
                get { return typeof(ChoiceField); }
            }
     
            /// <summary>
            /// Configures the specified definition.
            /// </summary>
            /// <param name="definition">The definition.</param>
            public override void Configure(IFieldDefinition definition)
            {
                // retrieve Control Definition Name to parse dynamic type of the module
                var controlDefinitionName = definition.ControlDefinitionName.Split('.');
                if (controlDefinitionName == null || controlDefinitionName.Length == 0)
                    throw new NullReferenceException(string.Concat("Control Definition is empty or not found: ", definition.ControlDefinitionName));
     
                // re-join array minus last item to get full dynamic module type
                var moduleType = string.Join(".", controlDefinitionName.Take(controlDefinitionName.Length - 1));
     
                // retrieve fieldname from definition to get the specified dynamic type
                var fieldName = definition.FieldName;
                if (string.IsNullOrEmpty(fieldName))
                    throw new NullReferenceException("Field name cannot be empty.");
     
                // resolve the type, ensure it exists
                Type contentType;
                var dynamicModuleManager = DynamicModuleManager.GetManager();
                moduleType = string.Concat(moduleType, ".", fieldName);
     
                try
                {
                    contentType = TypeResolutionService.ResolveType(moduleType);
                }
                catch (Exception ex)
                {
                    throw new TypeLoadException(string.Concat(moduleType, " dynamic type not found."));
                }
     
                // retrieve published items and add to choice list
                var myCollection = dynamicModuleManager.GetDataItems(contentType).Where(g => g.Status == ContentLifecycleStatus.Live);
     
                var itemSelect = new ChoiceItem();
                itemSelect.Text = "Select";
                itemSelect.Value = "Select";
                this.Choices.Add(itemSelect);
     
                foreach (var contentItem in myCollection)
                {
                    var item = new ChoiceItem();
                    item.Text = contentItem.GetValue("UnitName").ToString();
                    item.Value = contentItem.Id.ToString();
                    this.Choices.Add(item);
                }
     
                base.Configure(definition);
            }
        }
    }

  6. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    03 Dec 2013
    Link to this post
    Hello,

    Thank you Mohanakrishnakumar for sharing your solution.

    Regards,
    Stefani Tacheva
    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 Public Issue Tracking system and vote to affect the priority of the items
  7. Mateen
    Mateen avatar
    13 posts
    Registered:
    29 Aug 2012
    04 Dec 2013 in reply to Mohanakrishnakumar
    Link to this post
    Thankx MohanakrishnaKumar
  8. shabana
    shabana avatar
    1 posts
    Registered:
    16 Jun 2016
    16 Jun
    Link to this post
    One can refer this article: http://javascriptstutorial.com/blog/selecting-dropdown-element-using-javascript-or-jquery/
  9. Tigger
    Tigger avatar
    17 posts
    Registered:
    25 Apr 2016
    02 Jul
    Link to this post

    Mohanakrishnakumar .... I am new to SF

    Would you mind explaining how the code snippet would be used  as part of a module create as a "dynamic module".    My understanding was that you would have to create a static module in order to obtain that control

     

    Thanks

     

  10. Pavel Benov
    Pavel Benov avatar
    21 posts
    Registered:
    29 Nov 2016
    14 Jul
    Link to this post
    Hi Tigger,

    I believe MohanakrishnaKumar has created a custom FieldControl which he added to the dynamic module:

    http://docs.sitefinity.com/tutorial-build-a-custom-field-control

    This scenario however can now be done with RelatedDataField which is available out of the box and provides UI for associating one content item with other one/s:

    http://docs.sitefinity.com/add-a-related-data-custom-field

    Regards,
    Pavel Benov
    Telerik by Progress
     
    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
     
  11. Tigger
    Tigger avatar
    17 posts
    Registered:
    25 Apr 2016
    15 Jul in reply to Pavel Benov
    Link to this post
    Pavel ...thanks for the clarification 
  12. Pavel Benov
    Pavel Benov avatar
    21 posts
    Registered:
    29 Nov 2016
    18 Jul
    Link to this post
    Hello Tigger,

    I am glad I could help.

    Regards,
    Pavel Benov
    Telerik by Progress
     
    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
     
12 posts, 0 answered