Drop down list in dynamic content

Vote:
 

Hi folks

I've successfully created a few dynamic content examples in the past, but these have only utilised the PropertyString and the PropertyPageReference.  Now I'd like to add a drop down list, but despite trying all morning, I can't figure out how to do this and I can't find any examples (as far as I'm concerned, dynamic properties is a black art and I've only managed to get them to work in the past by relying heavily on other peoples' example code).

Does anyone know the name of the property that creates a drop down list?  Is there such a property?

Cheers

#39796
Jun 02, 2010 7:00
Vote:
 

I suppose one way would be to create a custom property type and use that inside your dynamic content. You can extend PropertyString and override CreateEditControls to create a drop-down list. Take a look (using reflector) at the PropertySelectControlBase and it's subclasses for inspiration.

#39798
Jun 02, 2010 8:25
Vote:
 

Are you talking about Dynamic Content or Custom Properties?

#39799
Jun 02, 2010 9:02
Vote:
 

Thanks for your replies Magnus and Frederik - sorry I've taken so long to reply, but I decided to take another approach to this problem and so it's been some time since I've thought about it.

Frederik - in reply to your question, I was talking about Dynamic Content (I think), where you can create a class that implements IDynamicContent and it shows up in a little pop-up that appears when you select the curly brackets {} in the editor.  I've created a few of these utilising properties such as PropertyString (which creates an HTML textbox), PropertyLongString (creates an HTML textarea) and PropertyPageReference (creates an EpiServer page selector thingy).

Magnus - I suppose what you're saying is that unlike the examples above, there isn't a drop down list property and if I want to use one, I'll have to create it.  Thanks for your tips on how I might go about this, but I fear that this is perhaps a wee bit beyond my meagre programming abilities at this stage.  Still, if I find that in the future I really need a drop down list then no doubt I'll need to figure out what's going on.

Cheers folks, and thanks again.

#43638
Sep 21, 2010 3:11
Vote:
 
#43639
Sep 21, 2010 3:11
Vote:
 

Creating a custom property type like that is much easier than you think. You just have to create a class inheriting from PropertyString and decorate it with the PageDefinitionTypePlugIn attribute. Then override CreatePropertyControl to return an instance of a custom subclass of PropertySelectControlBase. In the custom subclass' SetupEditControls override, create and add list items and mark the current value as selected. Done!

This is the code for the language property type which displays the enabled lanugage branches in a dropdown and saves the selected value as a string:

[Serializable, PageDefinitionTypePlugIn]
public class PropertyLanguage : PropertyString
{
    public override IPropertyControl CreatePropertyControl()
    {
        return new PropertyLanguageControl();
    }
}
public class PropertyLanguageControl : PropertySelectControlBase
{
    protected override void SetupEditControls()
    {
        // Using language branches as source for values, you could of course use anything you like
        LanguageBranchCollection branchs = LanguageBranch.ListEnabled();
        string str = this.ToString(); // Gets the current value
        foreach (LanguageBranch branch in branchs)
        {
            ListItem item = new ListItem(branch.Name, branch.LanguageID);
            // Compare the current value of the property to the list item created and make sure the current value is selected in the dropdown
            item.Selected = str == branch.LanguageID;
            base.EditControl.Items.Add(item);
        }
    }
}
Then just use the cusom property type in the dynamic content like you used the built-in property types before.
The dropdown can be made more dynamic for example using the new property settings concept in CMS 6 to get the selectable values from a configurable source. However, that would mainly make page properties easily configurable, not so much for dynamic content.
#43645
Sep 21, 2010 7:03
Vote:
 

Sorry for the messed up text. I should learn to use the new code section instead :)

#43646
Sep 21, 2010 7:05
Vote:
 

You're correct Magnus, it was much easier to create a custom property than I thought; your example was really helpful and worked a treat.  Thank you.

Now all I have to do is to try and remember why I wanted to implement a drop down list in the first place (or dream up a new bit of Dynamic Content that might use one).

Cheers

#43649
Sep 21, 2010 7:39
Vote:
 

The solution above worked like a charm, but I've got another problem down the road.

This is the problem:

Lets say that create a custom property (e.g. a drop down with the values red, green & blue). If you (in your dynamic content) select the second value (green) in the drop down, saves and publish the page, you will get the correct data when rendering the page. But if you go in to EPi CMS and chooses to edit the dynamic conent (previously added), the value you selected earlier (green) will not be selected value in the drop down. How do I do to select the item in the drop down that is actually the choosen value?

Kind regards / Anders

#70277
Apr 16, 2013 14:53
Vote:
 

Hi Anders,

 

Are you using the State property to set and get the value selected in the drop down?  For example, say my custom drop down is something like this:

Public Class ImagePositionDropDownListControl
    Inherits PropertySelectControlBase
    Protected Overrides Sub SetupEditControls()
        Dim str As String = Me.ToString()
        Dim liLeft As New ListItem("Left", "left")
        liLeft.Selected = str = liLeft.Value
        MyBase.EditControl.Items.Add(liLeft)
        Dim liRight As New ListItem("Right", "right")
        liRight.Selected = str = liRight.Value
        MyBase.EditControl.Items.Add(liRight)
        Dim liCentre As New ListItem("Centre", "centre")
        liCentre.Selected = str = liCentre.Value
        MyBase.EditControl.Items.Add(liCentre)
    End Sub
End Class

    

I can then get and set the value as a string in the Dynamic Conent State property, like so (where the name of the instance of my custom drop down is ddPosition):

Public Property State As String Implements EPiServer.DynamicContent.IDynamicContent.State
        Get
            Return Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(ddPosition.ToString()))
        End Get
        Set(ByVal value As String)
            ddPosition.ParseToSelf(ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(value)))
        End Set
    End Property
End Class

    

 Sorry about the VB code, but that's unfortunately how it is in our project.  Hopefully that's more or less what you were after.

#70303
Apr 16, 2013 23:50
Vote:
 

Hello Tim!

Thanks a lot! string str = this.ToString(); (or as they say in the world of VB "Dim str As String = Me.ToString()") was the key to the solution. For some reason I missed that part in the example above. The correct item in the dropdown now gets selected when going in to edit the dynamic content.

Thanks again!

#70322
Edited, Apr 17, 2013 10:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.