Listing 2: C# • Modify the Template Code
I recommend modifying the WebPart Visual Studio 2005 template code as shown. Specifically, you should override the CreateChildControls method with (commented) code to create a Label that can be used immediately, as well as an example property, also commented and ready to use. This code is a close approximation to a "real world" WebPart used by developers.
[Guid("$guid2$")]
public class $safeprojectname$ :
System.Web.UI.WebControls.WebParts.WebPart
public class Web_Part1 :
System.Web.UI.WebControls.WebParts.WebPart
{
//Private Label lblLabel;
// TODO: Define properties here.
//private const string _defaultText =
//"One Property";
//private string _myProperty = _defaultText;
//[Personalizable(PersonalizationScope.Shared),
//WebBrowsable(true),
//WebDisplayName("One Property"),
//WebDescription("Defines One Property"),
//Category("Miscellaneous"),
//DefaultValue(_defaultText)]
//public string myProperty
//{
// get { return _myProperty; }
// set { _myProperty = value; }
//}
protected override void Render(
HtmlTextWriter writer)
{
// TODO: Add custom rendering code here.
// writer.Write("Output HTML");
}
protected override void CreateChildControls()
{
// TODO: Add custom CreateChildControls code here.
//lblLabel = new Label();
//lblLabel.Text = "my Label";
//this.Controls.Add(lblLabel);
}
}