Birger
Birger

Reputation: 359

Using dropdownlist i webpart (edit mode)

I'm trying to add a checkboxlist to a webpart. In the edit mode there will be a dropdown list that will contain field names from a list and the field selected will be used as the display for the checkboxlist entries in the webpart.

I have not be able to find any examples on how to get this working.

Upvotes: 0

Views: 1001

Answers (1)

skeletank
skeletank

Reputation: 2888

Use a custom ToolPart to create your dropdown property as such:

public class DropdownToolPart : ToolPart
{
  protected override void CreateChildControls()
  {
    DropDownList dropdownList = new DropDownList();

    // Code to add field names from SharePoint List to dropdownlist

    this.Controls.Add(dropdownList);

    base.CreateChildControls();
  }

  public override void ApplyChanges()
  {
    CheckBoxListWebPart myWebPart = 
      (CheckBoxListWebPart)this.ParentToolPane.SelectedWebPart;

    //You will need to get the selected value of the dropdown by finding it 
    //in the Controls collection.
    string selectedValue = ...    

    myWebPart.CheckBoxListDisplayField = selectedValue;
  }
}

Your WebPart should do the following to include the ToolPart:

public class CheckBoxListWebPart: WebPart
{
  public string CheckBoxListDisplayField { get; set; }      

  public override ToolPart[] GetToolParts()
  {
    ToolPart[] toolParts = new ToolPart[1];

    DropdownToolPart myToolPart = new ToolPart();
    toolParts[0] = myToolPart;

    return toolParts;
  }
}

From there you should be able to create your checkboxlist in the CreateChildControls method of the CheckBoxListWebPart. In there you will need to load the items from your SharePoint list and then use the CheckBoxListDisplayField value to select the exact field value from each item.

Upvotes: 1

Related Questions