Part 2: Build Test Web Site
Posted by Steve Pietrek on June 6, 2007
This is Part 2 of a series on creating web parts and deploying them to SharePoint using a VS 2005 Solution Project. There are 6 parts in the series.
1. Select File>Add>New Web Site from the menu
2. In the Add New Web Site dialog, select ASP.NET Web Site, put it in the “C:\Documents and Settings\%USER%\My Documents\Visual Studio 2005\WebSites\TestHelloWorldWebSite” folder, and click OK
3. Add the HelloWorldWebPart reference to the project. The easiest way to retrieve it is to select the Projects tab in the Add Reference dialog.
4. Open Default.aspx and register the HelloWorldWebPart assembly by adding the following line:
<%@ Register Assembly=”HelloWorldWebPart” Namespace=”HelloWorld.Web” TagPrefix=”portal” %>
5. The next step is to add the WebPartManager, DropDownList to switch display modes, a Personalization Reset link button, the Hello World web part, and an Editor Zone with editors.
6. Add the following html code to Default.aspx:
<form id=”form1″ runat=”server”>
<div>
<asp:WebPartManager ID=”WebPartManager2″ runat=”server”>
</asp:WebPartManager>
<asp:DropDownList ID=”DropDownList1″ runat=”server” AutoPostBack=”true” OnSelectedIndexChanged=”DisplayModeChangedCallback”
Width=”261px”>
</asp:DropDownList>
<asp:LinkButton ID=”LinkButtonReset” runat=”server” OnClick=”LinkButtonResetCallback”>Reset</asp:LinkButton>
<br />
<asp:WebPartZone ID=”WebPartZone2″ runat=”server” Height=”450px” Width=”444px”>
<ZoneTemplate>
<portal:HelloWorldWebPart ID=”HelloWorldWebPart1″ runat=”server” Title=”Hello World Title”
EmployeeName=”Steve Pietrek” />
</ZoneTemplate>
</asp:WebPartZone>
<asp:EditorZone ID=”EditorZone1″ runat=”server”>
<ZoneTemplate>
<asp:AppearanceEditorPart ID=”AppearanceEditorPart1″ runat=”server” />
<asp:BehaviorEditorPart ID=”BehaviorEditorPart1″ runat=”server” />
<asp:LayoutEditorPart ID=”LayoutEditorPart1″ runat=”server” />
<asp:PropertyGridEditorPart ID=”PropertyGridEditorPart1″ runat=”server” />
</ZoneTemplate>
</asp:EditorZone>
</div>
</form>
7. Right-click in the Default.aspx editor and select View Code (or open Default.aspx.cs)
8. Add the following code in the code-behind file:
public partial class _Default : System.Web.UI.Page
{
WebPartManager wpmgr;
protected void Page_Load(object sender, EventArgs e) { }
void Page_Init(object sender, System.EventArgs e) {
Page.InitComplete += new EventHandler(InitCompleteCallBack);
}
void InitCompleteCallBack(object sender, EventArgs e) {
wpmgr = WebPartManager.GetCurrentWebPartManager(this);
foreach (WebPartDisplayMode mode in wpmgr.SupportedDisplayModes) {
if (mode.IsEnabled(wpmgr)) {
DropDownList1.Items.Add(new ListItem(mode.Name, mode.Name));
}
}
}
protected void DisplayModeChangedCallback(object sender, EventArgs e) {
wpmgr.DisplayMode = wpmgr.SupportedDisplayModes[DropDownList1.SelectedValue];
}
protected void LinkButtonResetCallback(object sender, EventArgs e) {
wpmgr.Personalization.ResetPersonalizationState();
}
}
9. Build the Web Site, click the Start Debugging button, and add a Web.config when prompted.
10. Once the site displays (you may have to refresh a few times in case there is a timeout creating the ASPNETDB.MDF file), you will see the name “Steve Pietrek” displayed in the web part. This occurred because we set the default for the EmployeeName property in the Default.aspx page. To change it as a user would, select Edit from the drop down list, select Edit in the web part’s verb menu, and search for the Data Category in the Web Part Editor. Click OK.
11. Close the Browser to stop debugging.


