Class PageBase
BasePage for EPiServer templates. Supports encoding settings and language translation.
Inheritance
Namespace: EPiServer
Assembly: EPiServer.dll
Version: 10.10.4Syntax
public abstract class PageBase : Page, IPageSource, ICurrentPage, IContentSource, ISupportsScriptManager
Remarks
This class inherits from System.Web.UI.Page, which has the core functionality for setting up and rendering a standard Web form (.aspx file.) The PageBase class extends the functionality of the Page class with EPiServer-specific features as access to configuration settings (Configuration property), user information (CurrentUser property) and information about the current EPiServer page (CurrentPage property.)
The PageBase class is an abstract class, meaning it cannot be instantiated. This class is the base class of all template classes in EPiServer. TemplatePage is the most used class as base for Web forms.PageBase also implements the IPageSource interface, which enables you to retrieve other EPiServer pages easily.
note
If you use anything apart from templates that inherit from PageBase to create EPiServer page types, no dynamic content will be presented on the Web page.
Constructors
PageBase(Int32)
Initializes a new instance of the PageBase class.
Declaration
public PageBase(int options)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | options | The page options to enable. |
Remarks
The options parameter is a bitmap constructed from the OptionFlag of Page plugin classes from the EPiServer.Web.PageExtensions namespace.
PageBase(Int32, Int32)
Initializes a new instance of the PageBase class.
Declaration
public PageBase(int enable, int disable)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | enable | The page options to enable. |
System.Int32 | disable | The page options to disable. |
Remarks
The disable bitmap will override enabled options in case of conflicting options.
Properties
CurrentPage
Get page data for current page.
Declaration
public virtual PageData CurrentPage { get; set; }
Property Value
Type | Description |
---|---|
PageData | A PageData object, null if failure. |
Remarks
A little caution is necessary when using CurrentPage in the HTML part of Web forms, as CurrentPage is implemented from IPageSource and UserControlBase also implements IPageSource. This means that CurrentPage will have different meanings in the same Web form depending on whether it's used outside any included EPiServer Web custom control in general and outside any templated EPiServer Web custom control in particular.
Examples
The following code example demonstrates the usage of both <strong>CurrentPage</strong> and <strong>Configuration</strong>.
The example is used in EPiServer's sample Web site to send e-mail. <strong>Configuration</strong> is used
to initalize the mail server and also, together with <strong>CurrentPage</strong>, to send information to
the recipient of the e-mail.
protected TextBox To, From, Subject, Other;
protected void SendEmailButton_Click(object sender, System.EventArgs e)
{
if (!IsValid)
return;
MailMessage mail = new MailMessage();
mail.IsBodyHtml = false;
mail.From = new MailAddress(From.Text);
mail.To.Add(To.Text);
mail.Subject = Subject.Text;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(Other.Text).
Append("\n\n").
Append(SiteDefinition.Current.SiteUrl.Host).
Append(CurrentPage.LinkURL).
Append("\n\nMessage was sent from IP address ").
Append(Request.UserHostAddress).
Append(" at ").
Append(DateTime.Now.ToString());
mail.Body = sb.ToString();
new SmtpClient().Send(mail);
string key = "CloseWindow";
string script = "<script type='text/javascript'>window.returnValue = true;window.parent.close();</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), key, script);
}
The following example demonstrates the usage of <strong>CurrentPage</strong> on a page template file, outside of any
included controls.
<%= CurrentPage.PageName %>
The following example demonstrates the usage of <strong>CurrentPage</strong> on a page template file,
inside ContentFramework and Content controls.
<development:DefaultFramework id="DefaultFramework" runat="server">
<EPiServer:Content ID="NewsListing" Region="menuRegion" runat="server">
CurrentPage.PageName on a page template file, inside ContentFramework and Content controls:
<%= CurrentPage.PageName %>
...
The following example demonstrates the usage of <strong>CurrentPage</strong> inside an EPiServer templated control.
<EPiServer:Newslist ID=Newslistnew Pagelinkproperty="NewsContainer" runat="server" MaxCount='<%# GetNewsCount() %>'>
<NewsTemplate>
CurrentPage.PageName on a page template file, inside Newslist control (in NewsTemplate):
<%= CurrentPage.Page %>
</NewsTemplate>
</EPiServer:Newslist>
The following code example demonstrates the usage of <strong>CurrentPage</strong> and <strong>IsValue</strong>.
CurrentPage is used to read the content of two properties; NewsCount and MainImage. To safeguard the property access, the presence and possible contents of both properties are first tested using the IsValue method.
protected int GetNewsCount()
{
return IsValue("NewsCount") ? (int)CurrentPage["NewsCount"] : -1;
}
protected string StartPageImage
{
get
{
return IsValue("MainImage") ? (string)CurrentPage["MainImage"] : _startPageImage;
}
}
CurrentPageHandler
Gets or sets the current page handler.
Declaration
public ICurrentPage CurrentPageHandler { get; set; }
Property Value
Type | Description |
---|---|
ICurrentPage | The current page handler. |
Remarks
The page handler is used to retrieve the current page. By assignig your own implementation to this property you can take control over the process of retrieving the current page.
CurrentPageLink
Get the reference to the current page.
Declaration
public virtual PageReference CurrentPageLink { get; set; }
Property Value
Type | Description |
---|---|
PageReference | A PageReference to the current page. |
Remarks
Parses the query string / form parameters to get the page reference.
CurrentPageLink has no place inside templated controls, since the Container property is of the type PageTemplateContainer, which doesn't implement CurrentPageLink.Examples
The following code example demonstrates the usage of <strong>CurrentPageLink</strong> to
retrieve the <strong>PageData</strong> object.
PageData tmpPageData = GetPage(CurrentPageLink);
EditHints
Gets the list containing the names of properties that have been registered for full refresh.
Declaration
public IList<string> EditHints { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IList<System.String> |
Remarks
This list will be read by the FullRefreshPropertiesMetaData and output as a comma separated list that that will be collected by the edit interface client.
IsDefaultPageLink
Determine if the current page request was done without an "id=..." parameter.
Declaration
public bool IsDefaultPageLink { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Remarks
The system will handle a missing id parameter by using the pageStartId value from EPiServer.Configuration.
IsDesignMode
Gets a value indicating whether this instance is design mode.
Declaration
protected bool IsDesignMode { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
|
LocalizationService
The localizationService used by the various Translate methods on PageBase.
Declaration
protected virtual LocalizationService LocalizationService { get; set; }
Property Value
Type | Description |
---|---|
LocalizationService |
Remarks
Will return the static property Current if not set.
Locate
Gets the object used to resolve services part of the public API.
Declaration
public ServiceLocationHelper Locate { get; }
Property Value
Type | Description |
---|---|
ServiceLocationHelper |
Options
Gets the page extension options, which is a bitmap constructed from the OptionFlag of Page plugin classes from the EPiServer.Web.PageExtensions namespace.
Declaration
public int Options { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
PageTranslation
Gets or sets the page translation object.
Declaration
public PageTranslation PageTranslation { get; set; }
Property Value
Type | Description |
---|---|
PageTranslation | The page translation. |
ScriptManager
Gets the script manager.
Declaration
public ScriptManager ScriptManager { get; }
Property Value
Type | Description |
---|---|
ScriptManager | The script manager. |
Methods
AccessDenied()
The method that is invoked when the current users access rights are not suffcient for the operation that he tries to perform.
Declaration
public virtual void AccessDenied()
Remarks
This method will perform different functions depending on the authentication method in use. If forms authentication is used, then it will redirect the user to the login page as defined in web.config. If windows authentication is used, an Access Denied header (http status 401) will be sent to the client.
Examples
The following code example demonstrates the usage of <strong>AccessDenied</strong> to deny access to a user that is not
logged on. The example is taken from the Web user control LoginStatus.ascx, which is delivered with
EPiServer. It must be understood that the click event for ASP.NET Server control Login has been linked
to the function Login_Click, so that when the user clicks the Login button, this function is executed.
Note that the call to AccessDenied does not necessarily mean that the user is denied access - it can also
be used to present the user with a login dialog. This happens in Web user control QuickBar.ascx.
public abstract class LoginStatus : UserControlBase
{
protected System.Web.UI.WebControls.LinkButton Login;
protected void Login_Click( object sender, System.EventArgs e )
{
System.Security.Principal.IPrincipal user = Context.User;
if ( ( user != null ) && user.Identity.IsAuthenticated )
{
return;
}
PageBase.AccessDenied();
}
}
BuildUrlWithPageReference(String, PageReference)
Helper method to construct a URL with a page reference parameter
Declaration
public string BuildUrlWithPageReference(string url, PageReference pageLink)
Parameters
Type | Name | Description |
---|---|---|
System.String | url | The original URL |
PageReference | pageLink | Page that should be referenced by the URL |
Returns
Type | Description |
---|---|
System.String | A new URL |
Remarks
This method is primarily intended to be used with a URL read from the PageURL property of a PageData class. In some special cases you may need to retarget the URL to another page or another version of the same page.
CheckAccess()
Verify that access control requirements are met for the current web request.
Declaration
public void CheckAccess()
Remarks
This method is called as part of the PageBase.OnInit method. It determines the current user's access level and compares it to the current page. If the user does not have sufficient access, the PageBase.AccessDenied method will be called.
note
Will trigger an AccessDenied if the access requirements are not met.
Get<T>(ContentReference)
Shortcut to retrieve an IContent object from the currently configured IContentRepository specified by the ContentReference parameter.
Declaration
public T Get<T>(ContentReference contentLink)
where T : IContentData
Parameters
Type | Name | Description |
---|---|---|
ContentReference | contentLink | Reference to the content to retrieve. |
Returns
Type | Description |
---|---|
T | The IContent object requested. |
Type Parameters
Name | Description |
---|---|
T | The type of the content object to be retrieved. |
See Also
GetChildren(PageReference)
Retrieves a PageData listing based on supplied PageReference argument (from IPageSource).
Declaration
public virtual PageDataCollection GetChildren(PageReference pageLink)
Parameters
Type | Name | Description |
---|---|---|
PageReference | pageLink |
Returns
Type | Description |
---|---|
PageDataCollection |
Remarks
The IPageSource interface is implemented by many classes, such as PageBase (and its descendants), DataFactory, PageControlBase. You typically use the members of this interface to get information about the current page (the CurrentPage property) or use either the GetPage or GetChildren methods to retrieve page data.
IPageSource was created as an interface due to the fact that the GetPage and GetChildren methods normally have the same implementation independently of the class that implements IPageSource. The CurrentPage property has different meanings for different implementations. As an example, CurrentPage on the PageBase class refers to the currently loaded page (based on the ID in the query string).
note
Note: As IPageSource is an interface, it doesn't implement any of its own attributes or methods itself. This means that you must be aware of which class implementing IPageSource that you're dealing with. For example, PageBase.CurrentPage returns a PageData object for the current page, the same for UserControlBase. However, in a templated control such as EPiServer.Web.WebControls.PageList, CurrentPage instead means the current page in the iteration.
Examples
The following code example demonstrates a specialized implementation of <strong>IPageSource</strong> to set
custom access level restrictions to the pages returned.
public class RestrictedPageSource : IPageSource
{
AccessLevel RequiredLevel;
public RestrictedPageSource()
: this(AccessLevel.Read)
{
}
public RestrictedPageSource(AccessLevel requiredLevel)
{
RequiredLevel = requiredLevel;
}
public PageData GetPage(PageReference pageLink)
{
return DataFactory.Instance.GetPage(pageLink);
}
public PageDataCollection GetChildren(PageReference pageLink)
{
return DataFactory.Instance.GetChildren(pageLink);
}
public PageData CurrentPage
{
get { return null; }
}
}
GetChildren<T>(ContentReference)
Shortcut to retrieve an IContent listing from the currently configured IContentRepository.
Declaration
public IEnumerable<T> GetChildren<T>(ContentReference contentLink)
where T : IContentData
Parameters
Type | Name | Description |
---|---|---|
ContentReference | contentLink | Reference to parent content whose children to retrieve. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | Returns a collection of IContent directly below the content referenced by the ContentReference parameter. |
Type Parameters
Name | Description |
---|---|
T | The type of the content objects to be retrieved. |
See Also
GetPage(PageReference)
Retrieves a PageData object based on supplied PageReference argument (from IPageSource).
Declaration
public virtual PageData GetPage(PageReference pageLink)
Parameters
Type | Name | Description |
---|---|---|
PageReference | pageLink |
Returns
Type | Description |
---|---|
PageData | Returns the PageData object for the specified PageReference argument. |
Examples
The following code example demonstrates the usage of <strong>GetPage</strong> to retrieve the <strong>PageData</strong>
object for the current page.
EPiServer PageData tmpPageData = GetPage( CurrentPageLink );
The following code example demonstrates the usage of GetPage and CurrentPage to retrieve PageData information for the page specified in the EventsContainer attribute.
protected PageData EventRootPage
{
get
{
if (_eventRootPage == null)
{
if (CurrentPage["EventContainer"] != null)
{
_eventRootPage =
GetPage((EPiServer.Core.PageReference)CurrentPage["EventContainer"]);
}
}
return _eventRootPage;
}
}
GetPage(PageReference, LoaderOptions)
Retrieves a PageData object based on supplied PageReference argument (from IPageSource).
Declaration
public virtual PageData GetPage(PageReference pageLink, LoaderOptions loaderOptions)
Parameters
Type | Name | Description |
---|---|---|
PageReference | pageLink | The page link. |
LoaderOptions | loaderOptions | The loader settings that is used to load the correct language and version of the page. |
Returns
Type | Description |
---|---|
PageData | Returns the PageData object for the specified PageReference argument. |
Examples
The following code example demonstrates the usage of <strong>GetPage</strong> to retrieve the <strong>PageData</strong>
object for the current page.
EPiServer PageData tmpPageData = GetPage( CurrentPageLink );
The following code example demonstrates the usage of GetPage and CurrentPage to retrieve PageData information for the page specified in the EventsContainer attribute.
protected PageData EventRootPage
{
get
{
if (_eventRootPage == null)
{
if (CurrentPage["EventContainer"] != null)
{
_eventRootPage =
GetPage((EPiServer.Core.PageReference)CurrentPage["EventContainer"]);
}
}
return _eventRootPage;
}
}
IsOptionEnabled(Int32)
Determines whether the specific page extension option is enabled.
Declaration
public bool IsOptionEnabled(int option)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | option | The option to check. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
option is the OptionFlag value for one of the page extensions defined with the [PagePlugIn] attribute.
IsValue(String)
Determine if the named property exists and has a value.
Declaration
public bool IsValue(string propertyName)
Parameters
Type | Name | Description |
---|---|---|
System.String | propertyName | The name of the page property to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if the property exists with a valid (i e non-null) value. |
Remarks
IsValue can be seen as a safe way to check whether a property exists and has been assigned contents.
Examples
The following code example demonstrates the usage of <strong>CurrentPage</strong> and <strong>IsValue</strong>. <strong>CurrentPage</strong>
is used to read the content of two properties; NewsCount and MainImage. To safeguard the property access,
the presence and possible contents of both properties are first tested using the <strong>IsValue</strong> method.
protected int GetNewsCount()
{
return IsValue("NewsCount") ? (int)CurrentPage["NewsCount"] : -1;
}
protected string StartPageImage
{
get
{
return IsValue("MainImage") ? (string)CurrentPage["MainImage"] : _startPageImage;
}
}
LoadDisplayTemplate(BlockData)
Resolves and instantiates the web or user control for the BlockData.
Declaration
public virtual Control LoadDisplayTemplate(BlockData blockData)
Parameters
Type | Name | Description |
---|---|---|
BlockData | blockData | The block data to populate the control with. |
Returns
Type | Description |
---|---|
System.Web.UI.Control |
OnInit(EventArgs)
Raises the System.Web.UI.Control.Init event to initialize the page.
Declaration
protected override void OnInit(EventArgs e)
Parameters
Type | Name | Description |
---|---|---|
System.EventArgs | e | An System.EventArgs that contains the event data. |
OnPreRenderComplete(EventArgs)
Raises the System.Web.UI.Page.PreRenderComplete event after the System.Web.UI.Page.OnPreRenderComplete(System.EventArgs) event and before the page is rendered.
Declaration
protected override void OnPreRenderComplete(EventArgs e)
Parameters
Type | Name | Description |
---|---|---|
System.EventArgs | e | An System.EventArgs that contains the event data. |
Remarks
We override this method to make sure that EPiServers script manager can render the script blocks needed.
QueryAccess()
The access level that the current user holds to the current page.
Declaration
public virtual AccessLevel QueryAccess()
Returns
Type | Description |
---|---|
AccessLevel | The access level. |
Remarks
Will return the actual access level for the current page, or Read if there is no current page.
QueryDistinctAccess()
Verify that access control requirements are met for the current web request.
Declaration
public bool QueryDistinctAccess()
Returns
Type | Description |
---|---|
System.Boolean | True if access requirements are met. |
Remarks
This method simply checks that all bits set in RequiredAccess() are set in QueryAccess().
RegisterClientScriptFile(String)
Helper method to register a client script file. If the method is called multiple times using the same key, only a single instance of the file is registered.
Declaration
public void RegisterClientScriptFile(string rootRelativePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | rootRelativePath | The script file that should be referenced. |
Remarks
The relativePath will be used as the unique script key.
RequiredAccess()
Determine the access level that is required for the current web request.
Declaration
public virtual AccessLevel RequiredAccess()
Returns
Type | Description |
---|---|
AccessLevel | The resulting access level. |
Remarks
This is the default implementation that simply requests Read access, regardless of the type of requested operation.
ResolveUrlFromUI(String)
Resolves the path relative the UI directory.
Declaration
public string ResolveUrlFromUI(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path. |
Returns
Type | Description |
---|---|
System.String |
ResolveUrlFromUtil(String)
Resolves the path relative the Util directory.
Declaration
public string ResolveUrlFromUtil(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path. |
Returns
Type | Description |
---|---|
System.String |
SetCachePolicy()
Sets the cache policy for this request based on parameters as current user and parameters.
Declaration
protected virtual void SetCachePolicy()
Remarks
Override this method if you wish to customize the cache policy for a page. The output cache is turned on based on the following criteria:
1. The EPnCachePolicyTimeout in web.config is > 0.
2. The current user must not be logged on, aka Anonymous.
3. The request must be a GET type request. Hence, Postbacks and form postings will not be cached.
4. The current page must be the published version (the WorkID is == 0).
The cache parameters are fetched from web.config, more specifically the EPsCacheVaryByCustom and EPsCacheVaryByParams settings. Additionally, a dependency to the DataFactoryCache is set. When pages are changed, the cache is flushed. Cache item expiration is set to the HttpCacheExpiration setting, which is the number of seconds the item should reside in the cache, as long as the StopPublish value of the page is not less than the policy timeout (in which case, the StopPublish value is used).SetupVisitorGroupImpersonation()
Called to configure visitor group impersonation used from editing preview
Declaration
protected virtual void SetupVisitorGroupImpersonation()
SetValuesForPropertyControls(Control)
Get new data from property web controls into the actual property objects for all controls implementing IPropertyControl.
Declaration
public virtual void SetValuesForPropertyControls(Control control)
Parameters
Type | Name | Description |
---|---|---|
System.Web.UI.Control | control |
Remarks
Used to update PagaData values when saving a page.
Translate(String)
Translate the given string to the current language.
Declaration
protected virtual string Translate(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | A string to translate |
Returns
Type | Description |
---|---|
System.String | The translated string. |
Remarks
GetStringByCulture(String, CultureInfo) for more information on the format of the string to translate.
TranslateFallback(String, String)
Translate the given string to the current language. Will return supplied fallback string if no match is found.
Declaration
protected virtual string TranslateFallback(string key, string fallback)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | A string to translate |
System.String | fallback | The string to return if no match was found for key. |
Returns
Type | Description |
---|---|
System.String | The translated string. |
Remarks
GetStringByCulture(String, CultureInfo) for more information on the format of the string to translate.
TranslateForScript(String)
Translate the given string to the current language, in a script-safe format.
Declaration
protected virtual string TranslateForScript(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | A string to translate |
Returns
Type | Description |
---|---|
System.String | The translated string. |
Remarks
GetStringByCulture(String, CultureInfo) for more information on the format of the string to translate.
ValidatePageTemplate()
Makes sure that the right page template is used to present a page.
Declaration
public virtual void ValidatePageTemplate()
Remarks
This method is called as part of the page load process. If a page (as determined by the ID query string parameter on the URL) is accessed with an .aspx file that is not the correct page template file, this method will throw an exception of type EPiServerException. If you have a page type that should be displayed using different page templates, you should override this method and do your custom validation. A validation error should be signalled by throwing an EPiServerException.
Events
PageSetup
Exposes the PageSetup event which is raised when a new page instance is created.
Declaration
public static event PageSetupEventHandler PageSetup
Event Type
Type | Description |
---|---|
PageSetupEventHandler |
Remarks
Attach to this event to be able to attach your code to specific events in the page lifecycle, allowing for your page extension to extend any page that derives from PageBase with your custom code.
Explicit Interface Implementations
IContentSource.CurrentContent
Gets the content of the current page.
Declaration
IContent IContentSource.CurrentContent { get; }
Returns
Type | Description |
---|---|
IContent | The content of the current page. |