public class SampleToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
// First, override the CreateChildControls method. This is where we create the controls.
protected override void CreateChildControls()
{
// create a panel that will hold all of our controls
Panel toolPartPanel = new Panel();
// create the actual control
DropDownList sampleDropDown1 = new DropDownList();
sampleDropDown1.ID = "sampleDropDown1";
sampleDropDown1.Items.Add("Item 1");
sampleDropDown1.Items.Add("Item 2");
sampleDropDown1.Items.Add("Item 3");
toolPartPanel.Controls.Add(sampleDropDown1);
// finally add the panel to the controls collection of the tool part
Controls.Add(toolPartPanel);
base.CreateChildControls();
}
// Next, override the ApplyChanges method.
// This method is where we will persist the values that the user selects.
public override void ApplyChanges()
{
// get the parent webpart
SampleWebPart parentWebPart = (SampleWebPart)this.ParentToolPane.SelectedWebPart;
// loop thru this control's controls until we find the ones that we need to persist.
RetrievePropertyValues(this.Controls, parentWebPart);
}
// Recursive function that tries to locate the values set in the toolpart
private void RetrievePropertyValues(ControlCollection controls, SampleWebPart parentWebPart)
{
foreach (Control ctl in controls)
{
RetrievePropertyValue(ctl, parentWebPart);
if (ctl.HasControls())
{
RetrievePropertyValues(ctl.Controls, parentWebPart);
}
}
}
// Method for retrieving the values set by the user.
private void RetrievePropertyValue(Control ctl, SampleWebPart parentWebPart)
{
if (ctl is DropDownList)
{
if (ctl.ID.Equals("sampleDropDown1"))
{
DropDownList drp = (DropDownList)ctl;
if (drp.SelectedItem.Value != "")
{
parentWebPart.myProperty = drp.SelectedItem.Value;
}
}
}
}
}