Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom web part that has the default toolpart and one additional custom property. I have a use case that requires me to be able to access the value of Title within the web part (this.Title) so that I can change its value immediately after a user changes it. However, any place I call this.Title from within my code after Apply Changes is clicked will show the previous value of the title. How can I access the just-entered Title?

Example:

Title value is "My Title".

User clicks edit web part.

User changes the title to "Different Title".

User clicks "ok" or "apply changes".

My code grabs "My Title" instead of "Different Title", although the next time the page loads, it will grab "Different Title". :(

I've tried overriding various methods like OnInit, OnPreRender, OnLoad, and CreateChildControls, but it doesn't seem like I can access the just-updated property in the page lifecycle.

Is there a simple way I can access this property in my code? Thanks.

share|improve this question
    
Matt, Did you solve the issue? If one of the answers helped you out please mark as answered. –  Anita Boerboom Jun 13 '11 at 11:03
    
Yes, I'll add my own answer that specifically addresses the problem within the next day or so (as I'm busy at the moment). –  Matt Jun 13 '11 at 15:30

6 Answers 6

One solution is to set up the values for your web part in the OnPreRender event -- I've done so in an example of "Web Designer Friendly Web Parts" on SharePoint 2007. Since we didn't have a custom EditorPart, SyncChanges and whatnot wouldn't have worked anyway.

share|improve this answer

I've had this problem a lot with web parts I developed with jQuery code. The only way I got it to show after hitting the apply was to use the Render Event to display the web part. It's been a little bit of a hassle building the code here--but for my projects it's worked.

share|improve this answer

In the EditorPart the methods ApplyChanges and SyncChanges can be used to set your own title at all times:

YourWebpart wp = (YourWebpart)this.WebPartToEdit;
wp.Title = "Title from code";
share|improve this answer

That is because of the way SharePoint (or better, the ASP.NET framework) works. The events are fired in a certain order, the ApplyChanges is fired to late in the event sequence, so any update will only be shown after you refresh the page. Clicking Apply first and then OK would show the correct title though (apply sets the change, Ok just does it again but since it is already set it will show when you are the ok click redirects you back out of the edit screen.

share|improve this answer

You may also try overriding the OnEditModeChanged event of the base webpart class and set your propeties in that.

share|improve this answer
up vote -1 down vote accepted

To solve the problem I needed to create a custom tool part that displays a text box to represent the title of the web part:

public class CustomToolPart : ToolPart
{
    ...
}

So I don't show the title twice, I hid the original Title property:

public override ToolPart[] GetToolParts()
{
    ...

    WebPartToolPart webPartToolPart = new WebPartToolPart();
    webPartToolPart.Hide(WebPartToolPart.Properties.Title);

    ...

    return toolparts;
}

In CreateChildControls for CustomToolPart I accessed my web part by calling:

MyWebPart webPart = (MyWebPart)ParentToolPane.SelectedWebPart;

In my web part, I added a hidden property called "enteredTitle":

public class MyWebPart : WebPart
{
    ...

    /// <summary>
    /// Retains a copy of the string a user types into the title property of the toolpart before it gets modified
    /// by code in the CreateChildControls() method of this class.
    /// </summary>
    [WebBrowsable(false)]
    [WebPartStorage(Storage.Shared)]
    public string enteredTitle { get; set; }

    ...
}

I then set the entered title to the newly created hidden property in the ApplyChanges() of CustomToolPart:

public class CustomToolPart : ToolPart
{
    ...

    public override void ApplyChanges()
    {
        if (changed)
        {
            webPart.enteredTitle = textBox.Text;
        }
    }

    ...
}

When CreateChildControls() is called in MyWebPart I can write this.Title = this.enteredTitle to immediately display the updated title in the refreshed page.

This technique makes it easy to have control of the title at any stage the page lifecycle. This is especially useful when translations come into play; for example, when I want to manipulate the Title to display a string from a resource when the user types in a title beginning with the string $Resources:

public class MyWebPart : WebPart
{
    ...

        if (string.IsNullOrEmpty(this.enteredTitle))
            this.Title = base.Title;
        else if (enteredTitle.StartsWith("$Resources"))
        {
            try
            {
                this.Title = Tools.TranslateResourceString(enteredTitle);
                if (string.IsNullOrEmpty(this.Title))
                    this.Title = enteredTitle;
            }
            catch
            {
                this.Title = enteredTitle;
            }
        }
        else
            this.Title = enteredTitle;

    ...
}

/// <summary>
/// Translates a string in the form of $Resources:File,Key to grab a value from a resource string
/// stored within SharePoint.
/// </summary>
/// <param name="resourceReferenceString">The string to translate.</param>
/// <returns>Returns the value referenced by the resource string.</returns>
public static string TranslateResourceString(string resourceReferenceString)
{
    string s= resourceReferenceString.Substring(11);
    string[] args = s.Split(",".ToCharArray(), 2, System.StringSplitOptions.RemoveEmptyEntries);
    string newString = (string)HttpContext.GetGlobalResourceObject(args[0], args[1], Thread.CurrentThread.CurrentUICulture);
    return newString;
}
share|improve this answer
    
Bit of an unnecessary bodge IMHO. –  Ryan Jan 23 '12 at 16:53
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.