1.
Open Visual Studio 2005 and start a new C# class library.
2.
Add a reference to System.Web.
3.
Add the following using statements at the top of your file.
Listing 1
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
4.
Rename your class to Hello by renaming the class file, which should
rename that class name inside the file.
5.
Change the class to inherit from Web Part.
6.
Inside the class, override the base class RenderContents method.
7.
Replace the automatically added line referring to the base
rendercontents method with your own content.
8.
To show the fact that you are programming in the ASP.NET model you may
use one of the built in objects from the library.
9.
Below is an example of what the final product may look like.
Listing 2
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace HelloWorldWebPart
{
public class Hello : WebPart
{
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(@"Hi, " + this.Context.User.Identity.Name);
}
}
}