Take the 2-minute tour ×

I've got the following problem:

I created a WebPart with a ToolPart, this toolpart has multiple controls (textbox, dropdownlist, ...) when I fill in everything and apply, it all goes ok, even when i press ok. But when i go back to edit -> modify webpart, all my data i've entered is gone. How can i solve this?


share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You'll need to save the values from the Toolpart in the webpart's properties. For example, lets say I want to save a string for "Title"... in the webpart define a property:

private const string DEFAULT_WPPColumnTitle = "Title";
private string _WPPColumnTitle = DEFAULT_WPPColumnTitle;

public string WPPColumnTitle
    get { return this._WPPColumnTitle; }
    set { this._WPPColumnTitle = value; }

I always use the prefix "WPP" to keep all the web part properties together.

Then, in the Toolpart's ApplyChanges override, save the control's value (_ddlColumnsTitle) to the webpart (WPPColumnTitle):

/// <summary>
/// Called by the tool pane to apply property changes to
/// the selected Web Part.
/// </summary>
public override void ApplyChanges()
    // get our webpart and set it's properties
    MyCustomWebPart et = (MyCustomWebPart)ParentToolPane.SelectedWebPart;
    et.WPPColumnTitle = _ddlColumnsTitle.SelectedValue;

Lastly, if the user edited the properties already, we want the Toolpart to be pre-populated with the user's configuration. In the CreateChildControls() method of your Toolpart, initialize the controls:

protected override void CreateChildControls()
        MyCustomWebPart et = (MyCustomWebPart)ParentToolPane.SelectedWebPart;

        // ... code to create _ddlColumnsTitle and add it to the Controls

        // default our dropdown to the user's selection
        ListItem currentItem = _ddlColumnsTitle.Items.FindByValue(et.WPPColumnTitle);
        if (null != currentItem)
            _ddlColumnsTitle.SelectedValue = currentItem.Value;
    catch (Exception ex)
        _errorMessage = "Error adding edit controls. " + ex.ToString();
share|improve this answer
I've already got this. But my problem is when i apply, or press ok, and than go back to the edit mode of my webpart, all my properties are back default ... Maybe I need to override a function in my webpart when i go to edit mode? –  RubenHerman Nov 19 '10 at 8:47
Erm - do you still have the problem? Answer is marked as correct so not sure. –  Ryan Nov 19 '10 at 9:13
@Ruben, Sorry forgot the part about initializing the Toolpart with the user's previous selections. Looks like you were able to get it working anyways. I updated my answer for completeness.:) –  Kit Menke Nov 19 '10 at 15:14
add comment

Open up the debugger and double check that the values are getting applied to your propertries on Apply (i.e. WPPColumnTitle is set).

If so then problem is that SharePoint is not serializing/deserializing the value from the property (WPPColumnTitle) to the database and back - verify by writing out this property on the web part - as soon as you leave the page and come back it will be empty.

If so then check things like this on class

[XmlRoot(Namespace = "YourNamespace")]

and this (not strictly necessary) on properties

[XmlElement(ElementName = "ColumnTitle")]

I've also seen problems if you name your web part class "WebPart" so call it MyWebPart

share|improve this answer
add comment

I've solved it with adding a property in my webpart "IsNeverSet" (bool) and when i go to the "CreateControls()" of my toolpart, I get this property and if it's false, I load all the properties from my webpart and fill them in the toolpart. So I found it with the help of Kit Menke

share|improve this answer
add comment

Your Answer


