none
ENUM Dropdown List as a Custom Property in WebPart RRS feed

  • Question

  • how would i do if i have to show the value in the dropdownlist something like this:

    UTF-8,  ISO-8859-1

    below prop works find if there is no space or no hypen and i know there is one option to use underscore but does not seems look nice, so i am looking to display the value in dropdownlist UTF-8, ISO-8859-1

    public enum ddlEnum { option1, option2, option3 }
    [WebBrowsable(true),
    Category("Miscellaneous"),
    Personalizable(PersonalizationScope.Shared),
    WebDisplayName("Dropdown List Display Text")]
    public ddlEnum ddlProp { get; set; }

     



    • Edited by AbuHamzah Thursday, September 8, 2011 11:15 PM
    Thursday, September 8, 2011 11:13 PM

Answers

  • You need to create custom toolpart for this kind of scenrio

    Toolpart code

    ==============

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.SharePoint.WebPartPages;

    using System.Web.UI.WebControls;

    using Microsoft.SharePoint;

    namespace SteelPoint.CMS

    {

        class CustomToolPart : ToolPart

        {

            private DropDownList ddlRelationshipType;

     

            private void CustomToolPart_Init(object sender, System.EventArgs e)

            {

            }

     

            public CustomToolPart()

            {

                this.Init += new EventHandler(CustomToolPart_Init);

            }

     

            protected override void CreateChildControls()

            {

                base.CreateChildControls();

                renderTable();

            }

     

            public override void ApplyChanges()

            {

                EnsureChildControls();

                SendDataToWebPart();

            }

     

            private void SendDataToWebPart()

            {

                EnsureChildControls();

                BaseCustom wpCustom = (BaseCustom)this.ParentToolPane.SelectedWebPart;

     

                // Send the custom text to the Web Part.

                if (ddlRelationshipType != null)

                {

                    wpCustom.RelationTypeID = ddlRelationshipType.SelectedValue;

                }

            }

     

            public override void SyncChanges()

            {

                base.SyncChanges();

            }

     

            public override void CancelChanges()

            {

                base.CancelChanges();

            }

     

     

            protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)

            {

                base.RenderToolPart(output);

            }

     

     

            public void renderTable()

            {

                ListItem item;

     

                    ddlRelationshipType = new DropDownList();

     

                        ddlRelationshipType.Items.Add(new ListItem("Your value", "Your value"));

     

     

                    if (!string.IsNullOrEmpty(wpCustom.RelationTypeID))

                    {

                        item = ddlRelationshipType.Items.FindByValue(wpCustom.RelationTypeID);

                        if (item != null)

                        {

                            ddlRelationshipType.SelectedValue = item.Value;

                        }

                    }

                    Controls.Add(ddlRelationshipType);

                }

            }

        }

    }

    --------------------
    Webpart code
    ===========================
    namespace CustomWebPArt
    {
        [DefaultProperty("Text"), ToolboxData("<{0}:BaseCustomWebparts runat=server></{0}:BaseCustomWebparts>"), XmlRoot(Namespace = "SteelPoint.CMS.BaseCustomWebparts")]
        [XmlInclude(typeof(BaseCustomWebparts))]
        public class BaseCustomWebparts : Microsoft.SharePoint.WebPartPages.WebPart
        {
             
            # region Property declarations
           
            private String _strRelationTypeID = "0";
            
            
            [Browsable(false), Category(HEADER_LABEL),
            WebPartStorage(Storage.Shared),
            DefaultValue("defaultText"),
            FriendlyName("Relation Type"), Description("Text Property")]
            public String RelationTypeID
            {
                get { return _strRelationTypeID; }
                set 
                {
                    try
                    {
                        int iValue = Convert.ToInt32(value);
                        _strRelationTypeID = value; 
       
                    }
                    catch
                    {
                        _strRelationTypeID = "0"; 
                    }
                }
            }
         
            [WebPartStorageAttribute(Storage = Storage.Personal)]
            public override string Title { get; set; }
            
            #endregion
           
            
          
            protected override void CreateChildControls()
            {
                
            }
            public override ToolPart[] GetToolParts()
            {
                ToolPart[] toolparts = new ToolPart[3];
                WebPartToolPart wptp = new WebPartToolPart();
                CustomPropertyToolPart custom = new CustomPropertyToolPart();
                toolparts[1] = custom;
                toolparts[2] = new CustomWebpartsToolPart();
                toolparts[0] = wptp;
                return toolparts;
            }
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                
            }
          
            protected override void RenderWebPart(HtmlTextWriter output)
            {
                base.RenderWebPart(output);
            }
         
        }
    }


    Rahul Sharma
    http://sharepointarrow.blogspot.com/
    Saturday, September 10, 2011 7:02 AM

All replies

  • You need to create custom toolpart for this kind of scenrio

    Toolpart code

    ==============

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.SharePoint.WebPartPages;

    using System.Web.UI.WebControls;

    using Microsoft.SharePoint;

    namespace SteelPoint.CMS

    {

        class CustomToolPart : ToolPart

        {

            private DropDownList ddlRelationshipType;

     

            private void CustomToolPart_Init(object sender, System.EventArgs e)

            {

            }

     

            public CustomToolPart()

            {

                this.Init += new EventHandler(CustomToolPart_Init);

            }

     

            protected override void CreateChildControls()

            {

                base.CreateChildControls();

                renderTable();

            }

     

            public override void ApplyChanges()

            {

                EnsureChildControls();

                SendDataToWebPart();

            }

     

            private void SendDataToWebPart()

            {

                EnsureChildControls();

                BaseCustom wpCustom = (BaseCustom)this.ParentToolPane.SelectedWebPart;

     

                // Send the custom text to the Web Part.

                if (ddlRelationshipType != null)

                {

                    wpCustom.RelationTypeID = ddlRelationshipType.SelectedValue;

                }

            }

     

            public override void SyncChanges()

            {

                base.SyncChanges();

            }

     

            public override void CancelChanges()

            {

                base.CancelChanges();

            }

     

     

            protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)

            {

                base.RenderToolPart(output);

            }

     

     

            public void renderTable()

            {

                ListItem item;

     

                    ddlRelationshipType = new DropDownList();

     

                        ddlRelationshipType.Items.Add(new ListItem("Your value", "Your value"));

     

     

                    if (!string.IsNullOrEmpty(wpCustom.RelationTypeID))

                    {

                        item = ddlRelationshipType.Items.FindByValue(wpCustom.RelationTypeID);

                        if (item != null)

                        {

                            ddlRelationshipType.SelectedValue = item.Value;

                        }

                    }

                    Controls.Add(ddlRelationshipType);

                }

            }

        }

    }

    --------------------
    Webpart code
    ===========================
    namespace CustomWebPArt
    {
        [DefaultProperty("Text"), ToolboxData("<{0}:BaseCustomWebparts runat=server></{0}:BaseCustomWebparts>"), XmlRoot(Namespace = "SteelPoint.CMS.BaseCustomWebparts")]
        [XmlInclude(typeof(BaseCustomWebparts))]
        public class BaseCustomWebparts : Microsoft.SharePoint.WebPartPages.WebPart
        {
             
            # region Property declarations
           
            private String _strRelationTypeID = "0";
            
            
            [Browsable(false), Category(HEADER_LABEL),
            WebPartStorage(Storage.Shared),
            DefaultValue("defaultText"),
            FriendlyName("Relation Type"), Description("Text Property")]
            public String RelationTypeID
            {
                get { return _strRelationTypeID; }
                set 
                {
                    try
                    {
                        int iValue = Convert.ToInt32(value);
                        _strRelationTypeID = value; 
       
                    }
                    catch
                    {
                        _strRelationTypeID = "0"; 
                    }
                }
            }
         
            [WebPartStorageAttribute(Storage = Storage.Personal)]
            public override string Title { get; set; }
            
            #endregion
           
            
          
            protected override void CreateChildControls()
            {
                
            }
            public override ToolPart[] GetToolParts()
            {
                ToolPart[] toolparts = new ToolPart[3];
                WebPartToolPart wptp = new WebPartToolPart();
                CustomPropertyToolPart custom = new CustomPropertyToolPart();
                toolparts[1] = custom;
                toolparts[2] = new CustomWebpartsToolPart();
                toolparts[0] = wptp;
                return toolparts;
            }
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                
            }
          
            protected override void RenderWebPart(HtmlTextWriter output)
            {
                base.RenderWebPart(output);
            }
         
        }
    }


    Rahul Sharma
    http://sharepointarrow.blogspot.com/
    Saturday, September 10, 2011 7:02 AM
  • Hi,

    Tried to use this in SharePoint 2007, to create a dropdown list (of lists) in the Web Task Panel, and came across two problems:

    1.  Where is BaseCustom defined, or is this a .Net 4.0 property?

    2. on line   if (!string.IsNullOrEmpty(wpCustom.RelationTypeID))

     - wpCustom doesn't exist in the current context, you've missed a definition or should wpCustom have be definited globally?

    Thanks

    David Hughes

    Further tweeking BaseCustom should have been BaseCustomWebParts,  yes defining wpCustom globally solved te second headache but  it still didn't work, Error " the server was unable to load the webpart".

    • Edited by DiaGeordie Friday, November 18, 2011 2:46 PM Updating progress
    Friday, November 18, 2011 11:33 AM