Everything SharePoint and XAML

My continuous learning of SharePoint, XAML, Silverlight, Windows Phone 7, Windows 8, Office, VSTO, C#…

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.

Advertisements

One Response to “Part 2: Build Test Web Site”

  1. Deepak said

    I am unable to debug (step # 9)
    I am getting this error

    Server Error in ‘/TestHelloWorldWebSite’ Application.
    ——————————————————————————–

    A page can have only one server-side Form tag.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: A page can have only one server-side Form tag.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [HttpException (0x80004005): A page can have only one server-side Form tag.]
    System.Web.UI.Page.OnFormRender() +8905736
    System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +36
    System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31
    System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +53
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
    System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
    System.Web.UI.Control.Render(HtmlTextWriter writer) +10
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
    System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +173
    System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31
    System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +53
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
    System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
    System.Web.UI.Control.Render(HtmlTextWriter writer) +10
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
    System.Web.UI.Page.Render(HtmlTextWriter writer) +29
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

Sorry, the comment form is closed at this time.

 
%d bloggers like this: