I was able to do this "successfully". Here are some of the things I did that were different than above:
First - common sense - in the section Defining the logic and rendering of your webpart . Make sure to remove all of the <b> and </b> tags...OK that's a bit of entertainment as well.
If you attempt to build and see several errors "Caption is deprecated..." etc. check your "Using" directives and make sure they match the ones in the example. It may be overkill, but here are the contents of my file:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
namespace MyWebParts
{
[Guid("60487385-c231-4274-8536-64e5be432bd5")]
//public class SimpleWebPart : System.Web.UI.WebControls.WebParts.WebPart
[XmlRoot(Namespace="MyWebParts")]
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "hello";
private string text = defaultText;
// Declare variables for HtmlControls user interface elements.
HtmlButton _mybutton;
HtmlInputText _mytextbox;
// Event handler for _mybutton control that sets the
// Title property to the value in _mytextbox control.
public void _mybutton_click(object sender, EventArgs e)
{
this.Title = _mytextbox.Value;
try
{
this.SaveProperties = true;
}
catch
{
//Caption = "Error... Could not save property.";
this.ToolTip = "Error... Could not save property.";
}
}
// Override the ASP.NET Web.UI.Controls.CreateChildControls
// method to create the objects for the Web Part's controls.
protected override void CreateChildControls()
{
// Create _mytextbox control.
_mytextbox = new HtmlInputText();
_mytextbox.Value = "";
Controls.Add(_mytextbox);
// Create _mybutton control and wire its event handler.
_mybutton = new HtmlButton();
_mybutton.InnerText = "Set Web Part Title";
_mybutton.ServerClick += new EventHandler(_mybutton_click);
Controls.Add(_mybutton);
}
[Browsable(true), Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"), Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
//protected override void Render(HtmlTextWriter writer)
protected override void RenderWebPart(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
RenderChildren(writer);
// Securely write out HTML
writer.Write("<BR>Text Property: " + SPEncode.HtmlEncode(Text));
}
}
}
To register a Web Part assembly as a SafeControl
When following the WebPart Library template instructions there are several options for ensuring the control is seen by WSS 3.0 as "safe". I initially edited the web.config in the root c:\inetpub\wwwroot directory - as it says in the instructions. When I imported the web part the import was successful (it showed up as an option on the import page), but dropping the webpart on the web part page failed with the message:
"Unable to add selected web part(s). A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe." Here's a good description of how to make a control "safe" in Sharepoint's eyes. http://msdn2.microsoft.com/en-us/library/ms916848.aspx
So, I edited the web.Config at the root of my site, C:\Inetpub\wwwroot\wss\VirtualDirectories\5219, for example using the XML provided in the example:
<SafeControl
Assembly="SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def148956c61a16b" Namespace="MyWebParts"
TypeName="*" Safe="True" />
.... repeated the import and was then able to drop the web part on the webpart page. IT WORKED! And I could use it to change the 'Title" of the Web Part itself. Cool!
There are likely some elements of the file (above), for example in the "using" directives, that may not be quite kosher for WSS 3.0. I'm new to web parts and WSS 3.0 so I do not know (yet) what these are.
I hope this helps.