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


Comments (11)

  1. Shalini says:

    Thats a beautiful post,working perfectly well

  2. Vivek says:

    Hi Experts,

    Recently i got this idea from the inetrnet post that we can use the web forms created in the asp.net in the sharepoint site. To use this we take the page viewer web part & put the url of the page but i have one problem.

    I need some web forms on my sharepoint site, to do this i create the web site in the visual studio 2005.

    Click->start->programs->visual studio 2005->new-> website.

    I created a simple form having a table & some text boxes now i created a folder in the "C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS" & copied the .aspx & .cs file the folder.

    Now the form exist in the layout folder of sharepoint but when i put the same form on my site by using the page viewer web part & the path is "_layoutsfoldernamemypage.aspx" but it shows error "It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. (C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12templatelayouts..web.config line 46)".

    Please help me to sort out the error

    Thank you

  3. saurabh says:

    Well…..its looking gud…..but how can i add WebpartManager and Webpart zone in this…..what i m doing is….i have crated a master page using sharepoint designer now i want to add an aspx page using Sharepoint designer in that MasterPage, in my  aspx page i want to add Webpart Zone …..but it doesn’t work….( m trying to add asp:webpartzone) but doesn’t work…..what i have to do…..plz ….help me out…..

  4. Vikram Vyas says:

    Hi..even i tried it..its not working for me..Jason i also tried changing trust level to medium, still no luck..it giving me unknow error…

    can some body check for web.config settings…

  5. jwidener says:

    I followed the article to the letter and I’m getting the error:

    It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. (C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12templatelayoutsggmitg_documentsdownloadweb.config line 31)

    When I get rid of the web.config file I get "Unknown Error". Any ideas?????

  6. Rama says:

    That was a pretty straightforward solution to my problem of setting master page to an application page.

    Very useful stuff

    Thanks

  7. Eric Legault says:

    Fantastic walkthrough Pranab, this has come in very handy.  I feel stupid though – every time I try to add an <asp:button> control, the page errors out!  What am I missing here?  Also, do you know how these pages can be debugged?

  8. Jason says:

    Hey.. I got it to work finally.I had to change the trust level to medium in web.config and run iisreset at command prompt (I’m not sure of the difference between running it from command prompt and from the IIS MMC, but it doesnt work if I restart from IIS MMC).This article helped a great deal and so was Chris’s and Noticia’s blog- so Thanks a ton

  9. Jason says:

    Pranab

    I get the same error as DS, is there an entry missing in web.config ? I’ve been trying several ways of getting this done with no luck yet.

    Jason

  10. Denis Simpson says:

    Hi pranab,

    I applied your instructions to the letter but when I browse to my page I get a SharePoint Unknown Error page instead.

    Are you sure there is nothing to add to the web.config of the site ?

    DS

Skip to main content