How to implement a Postback Enabled Custom aspx page in SharePoint 2007 (MOSS/WSS) Layouts Directory using Masterpage from any site

Here are the steps to follow:

1. Start a new Web Application using Visual Studio 2005 and go to the Property_Pages of the Website. Go to the MSBuild Options and in Output Folder Text Box change the location to the Bin directory of the SharePoint Web Application under which your target site is located. In my case it is: C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

 

2. Remove the default.aspx page and add a new Web Form name: testmaster.aspx. Add an autopostback enabled RadioButton List with 2 options yes and no, and a label into the page. My target is to change the text of the label with the selected value from the RadioButton List.

 

3. Here is the content of the testmaster.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testmaster.aspx.cs" Inherits="testmaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

   <div>

        <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"

            RepeatDirection="Horizontal" Width="177px">

            <asp:ListItem Selected="True">Yes</asp:ListItem>

            <asp:ListItem>No</asp:ListItem>

        </asp:RadioButtonList><br />

        &nbsp;</div>

        <asp:Label ID="Label1" runat="server" Width="168px"></asp:Label>

    </form>

</body>

</html>

 

4. Here is the content of the testmaster.aspx.cs file:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class testmaster : System.Web.UI.Page

{

   

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Label1.Text = RadioButtonList1.SelectedValue;

    }

}

5. Now build and debug this Web application to check whether it is working as expected.

 

6. Add reference to Windows SharePoint Services into the project.

 

7. Add few more lines to the testmaster.aspx.cs file. The final code structure is:

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

public partial class testmaster : System.Web.UI.Page

{

    protected override void OnPreInit(EventArgs e)

    {

        base.OnPreInit(e);

        SPWeb MyWeb = SPControl.GetContextSite(Context).OpenWeb();

        string StrURL = MyWeb.ServerRelativeUrl + "/_catalogs/masterpage/default.master"; //this will

        this.MasterPageFile = StrURL;

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Label1.Text = RadioButtonList1.SelectedValue;

    }

}

8. Build the application once again.

 

9. Now open the physical LAYOUTS folder (located at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS). Create a new folder name MyFolder under it. Copy the testmaster.aspx and testmaster.aspx.cs file from the original location to the MyFolder folder.

 

10. Open the testmaster.aspx from MyFolder folder in Notepad and make some necessary changes. Here is the final output:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testmaster.aspx.cs" Inherits="testmaster" %>

<asp:Content contentplaceholderid="PlaceHolderMain" runat="server">

<div>

<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"

OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"

RepeatDirection="Horizontal" Width="177px">

<asp:ListItem Selected="True">Yes</asp:ListItem>

<asp:ListItem>No</asp:ListItem>

</asp:RadioButtonList><br />

&nbsp;</div>

<asp:Label ID="Label1" runat="server" Width="168px"></asp:Label>

</asp:Content>

11. Now open browser and type http://<your site url>/_layouts/MyFolder/testmaster.aspx. You will find a postback enabled aspx page using default.master page of the target site.

I got the idea from this nice post: SharePoint 2007: using the masterpage from your site in custom _layouts pages