แชร์ผ่าน


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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://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 https://<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

  • Anonymous
    July 03, 2007
    PingBack from http://www.virtual-generations.com/2007/07/04/sharepoint-link-love-07-03-2007/

  • Anonymous
    August 31, 2007
    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

  • Anonymous
    September 04, 2007
    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

  • Anonymous
    September 06, 2007
    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

  • Anonymous
    October 08, 2007
    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?

  • Anonymous
    October 29, 2007
    That was a pretty straightforward solution to my problem of setting master page to an application page. Very useful stuff Thanks

  • Anonymous
    December 19, 2007
    The comment has been removed

  • Anonymous
    May 06, 2008
    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...

  • Anonymous
    May 22, 2008
    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.....

  • Anonymous
    September 04, 2009
    The comment has been removed

  • Anonymous
    September 09, 2009
    Thats a beautiful post,working perfectly well