Question

I have a custom web part (Contact Info) with a property (Topic). The topic is a drop down list (ie behind the scenes its an enum type. What I would like is for the values of this dropdown property to come from the values in a column of a list on my sharepoint site. I have a method working that uses CAML to fetch a list of the values from my list. However I do not know how to use these values to fill the dropdown property of my webpart.

How do I set the values of my enum property at run time with values pulled from a list?

Was it helpful?

Solution

You will need to create Custom ToolPart to dynamically populate the dropdown values.You will need to create a class file in the WebPart and Inherit the ToolPart class and then create the dropdown and populate the dropdown and override the child controls and apply changes method of WebPart. Below is an example where i populated the DroppDown with all the Lists in the site.

class DropDownTool : Microsoft.SharePoint.WebPartPages.ToolPart
{
    DropDownList ddlListProperties = new DropDownList();

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        this.Title = "Select List from DropDown";
        Literal lit = new Literal();
        lit.Text = "<br/><br/>";
        Controls.Add(ddlListProperties);
        Controls.Add(lit);
    }
    public DropDownTool(string _ddlValue)
    {
        SPListCollection listColl = SPContext.Current.Web.Lists;
        foreach (SPList list in listColl)
        {
            ddlListProperties.Items.Add(list.Title);
        }
        if (!string.IsNullOrEmpty(_ddlValue))
        {
            ddlListProperties.SelectedValue = _ddlValue;
        }
    }

    public override void ApplyChanges()
    {
        base.ApplyChanges();

        Webpart_Assignment.Webpart_Assignment.dynamicValue = ddlListProperties.SelectedValue;

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top