Programmatically setting the DropDownList in value method - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Programmatically setting the DropDownList in value method

  • Programmatically setting the DropDownList in value method
  • 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>
  • 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
  • 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
  • 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
  • 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);
            
        

  • 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
  • Thankx MohanakrishnaKumar
  • One can refer this article: javascriptstutorial.com/.../
  • 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

     

  • 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
     
  • Pavel ...thanks for the clarification 
  • 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