Step 1: Create a custom control by inheriting from the
WebControl
class and the override the RenderContents
method with your implementation.
In the below code snippet, we are just writing the site title and site URL to the browser.using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
namespace NameSpaceCustomControl
{
public class CustomControl1 : WebControl
{
protected override void RenderContents(HtmlTextWriter output)
{
SPWeb site = SPContext.Current.Web;
output.Write("The Site title is " + site.Title);
output.Write("<br/>");
output.Write("The URL of the site is " + site.Url);
}
}
}
Step 2: Compile the custom control class and generate the DLL. Once the compiled DLL is generated, register it in GAC.Step 3: Refer to the custom control assembly in your ASPX page using the
Register
attribute. Please remember to specify the public key token of the GAC.<%@ Register Assembly="CustomControl,Version=1.0.0.0,Culture=neutral,PublicKeyToken=4adae03f3c0d5b8e"
Namespace="NameSpaceCustomControl" TagPrefix="CustomSitePages" %>
Specify the custom control with a proper unique ID.<CustomSitePages:CustomControl1 ID="cc1" runat="server" />
Below is the complete code of the page:<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" meta:progid="SharePoint.WebPartPage.Document"%>
<%@ Register Assembly="CustomControl,Version=1.0.0.0,Culture=neutral,PublicKeyToken=4adae03f3c0d5b8e"
Namespace="NameSpaceCustomControl" TagPrefix="CustomSitePages" %>
<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain">
<h3>Hi this is a Site Page</h3>
<CustomSitePages:CustomControl1 ID="cc1" runat="server" />
</asp:Content>
Now if you run the page, you can see the custom control in action.
0 comments:
Post a Comment