0

Hi I am creating a visualwebpart in sharepoint 2010 with some webpart property. In webpart property I am trying to creating a dropdown using enum.

public enum FileTypeSupport
    {            
        OnlyImage,
        OnlyVideo,
        ImageAndVideo
    };
    public static FileTypeSupport fileType;

    [Personalizable(PersonalizationScope.Shared),
    Category("Caustom Property"),
    WebBrowsable(true),
    WebDisplayName("File Type Support"),
    Description("Specify the number of image which will show at a time")]
    public FileTypeSupport SelectedfileType
    {
        get { return fileType; }
        set { fileType = value; }
    }

This is working fine but my requirement is to dropdown of {Only Image, Only Video,Image And Video}, i.e with some space(OnlyImage-->Only Image), but it is not supporting in enum. Can anyone tell me how can I fullfill that requirement.

Arumoy Roy
  • 108
  • 12

1 Answers1

0

IF I understand you right here you want to display "Only Image" when "OnlyImage" is selected?

Then you have to possibilities: Posibillity ONE: Use a

public Dictionary<enum,string> SelectedFileType {

}

Where the enum contains: (FileTypeSupport.OnlyImage, "Only Image")

Possibility TWO: If you have exact rules you can modify the getter and setter:

public string SelectedfileType
{
get { return Regex.Replace(fileType.ToString(), "([a-z])([A-Z])", "$1 $2")); }
set { fileType=enum.Parse(typeOf(FileTypeSupport),value.Replace(" ","");
}

I stole the Getter from this SO answer. You can also find other answers there without regex which have a better performance. (But performance should not really be a problem as you use SharePoint)

Community
  • 1
  • 1
Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • I am trying to show "Only Image" in dropdown. generally dropdown shows all the values defined in enum but enum doesnot allow a value with space. – Arumoy Roy May 29 '15 at 11:32
  • Then my guess was right. Just do what I posted there. 2nd Option would maybe be the best. (The getter Inserts empty spaces, the setter removes them). So it should look nice on the frontend – Ole Albers May 29 '15 at 12:19
  • But then it will not show as dropdown anymore as it's return type is string. I need dropdown not a textbox. – Arumoy Roy May 29 '15 at 12:41
  • 1
    That's right. You will have to create your own custom control. You can't do this with default functionality. Another nice way using attributes to do this (but still requires custom controls) is answered there: http://sharepoint.stackexchange.com/questions/77674/display-names-in-dropdown-in-webpart-property – Ole Albers May 29 '15 at 13:02
  • 1
    Yah finally I decided to use EditorPart, No other way I found to fullfill the requirement. Thanks anyway. – Arumoy Roy May 29 '15 at 13:56