Web Part Properties

Ok, this is really basic... I use this bit of code all the time. I don't know much about blogging, so this is going to serve as a bit of a lesson for me too.

First, declare the variable in camel case:

private string myString = "Hello World!";

Then underneath, you set the properties.

[WebBrowsable(true), WebPartStorage(Storage.Shared)]
[Personalizable(PersonalizationScope.Shared)]
[FriendlyNameAttribute("My String")]
public string MyString
{
get { return myString; }
set { myString = value; }
}

This all goes before you start coding your methods.

To see the string itself in the web part, add the following method into your code:

protected override void Render(HtmlTextWriter writer)
{
writer.Write(MyString);
}

Now, when you deploy the web part and edit the web part properties, you'll see a new category, Miscellaneous. There, you can edit the string and save it. When you save it, whatever you edited that property to should appear in the web part.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.