开发者

Loss of properties webpart toolpart moss 2007

I've got the following problem:

开发者_如何学C

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?

Thanks


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;

[Browsable(false)]
[WebPartStorage(Storage.Shared)]
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()
{
    try
    {
        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();
    }
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜