1
votes

I have been searching trying to figure this out, but either I am not using the right terms or I'm just not finding the answer. If this has been asked and answered before my apologies for not finding it - I'd appreciate a link if the answer is already out there.

I have a visual webpart with custom properties (webpart class and user control) - all properly decorated with the WebBrowsable(true), WebPartStorage(Storage.Shared), etc. attributes. I have also tried Personalizable(Personalization.Shared) just for completeness. What I would like to do is, instead of having these webpart properties edited in the standard Edit Web Part interface, I want to show edit controls for them based on whether or not the current user is a site administrator.

Here's where my problem lies: when I set the custom properties from the user control the settings are not being saved. I have put a break point in my setter method in the webpart class and verified that a value is indeed being passed, but when the webpart is reloaded on the page the setting is back to the default of an empty string.

I feel like I am missing some basic step to cause these settings to be saved. Below are some snippets to demonstrate the code I'm using. This is not the entire code, obviously, but just the relevant parts to illustrate where I'm at.

SafetyTrackerControl.ascx.cs:

public partial class SafetyTrackerControl : System.Web.UI.UserControl
{
    public string EmployeeComments { get; set; }
    public SafetyTrackerWebpart ParentWebPart { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmployeeComments.Text = EmployeeComments;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ParentWebPart.EmployeeComments = txtEmployeeComments.Text;
    }
}

SafetyTrackerWebPart.cs

[XmlRoot(Namespace = "SafetyTrackerSettings")]
public class SafetyTrackerWebPart : WebPart
{
    SafetyTrackerControl _SafetyTrackerControl;

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        _SafetyTrackerControl = (SafetyTrackerControl)Page.LoadControl("~/_ControlTemplates/SafetyTracker/SafetyTrackerControl.ascx");
        _SafetyTrackerControl.EmployeeComments = EmployeeComments;
        _SafetyTrackerControl.ParentWebPart = this;
    }

    internal string _employeeComments;
    [WebBrowsable(true),
    WebDisplayName("Additional Comments"),
    WebPartStorage(Storage.Shared)]
    public string EmployeeComments
    {
        get { return _employeeComments; }
        set { _employeeComments = value; }
    }
}
1

1 Answers

0
votes

If your webpart inherits from Microsoft.SharePoint.WebPartPages.Webpart, you may try using SPWebPartManager class and have a look at SaveChanges method.

But i think that right approach would be creating control, that inherits from EditorPart class, and there decide if user should be able to edit this following property, or not in Edit webpart panel. You should also override CreateEditorParts method to return your custom edit panel. Oh, and you also should set WebBrowsable(false) attribute, so that default editor textbox would not be generated.