Using custom web part layouts

Layouts allow you to customize the appearance of web parts or add content. Web part layouts are custom skins that replace the HTML code and ASPX markup of the web part.

  • Each web part has its own list of layouts.
  • You can select a different layout for every instance of the web part.

To manage the layouts of web parts, open the Web parts application and edit web parts on the Layouts tab.

Requirement

The Control declaration in the markup of the web part’s user control file must contain the full relative path to the code behind file in the CodeFile attribute (CodeBehind attribute on web application installations). For example:




<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_Navigation_cmslistmenu" CodeFile="~/CMSWebParts/Navigation/cmslistmenu.ascx.cs" %>


We do not recommend calling any inline code in the web part layout file itself. Such custom code can be hard to maintain across different versions of Kentico if the code of the underlying web part changes. Only use the web part layout to extend or reorganize the web part’s markup or HTML.

If you need to call custom code, create a custom web part based on the original.

Do not remove controls from the layout

Keep all of the default controls inside the code of web part layouts to ensure that web parts work correctly. If you need to hide any of the controls, add the Visible=“False” attribute.

Note: Your custom web part layouts may not work after upgrading to a new version of Kentico (if the controls in the web part’s markup changed). Please test your website carefully after upgrading, and update your custom layouts according to the controls in the default layout if necessary.

Assigning layouts to web part instances

To select the layout for specific web part instances:

  1. Open the Pages application.
  2. Edit the page containing the web part instance on the Design tab.
  3. Configure (double-click) the web part instance.
  4. Switch to the Layout tab of the web part configuration dialog.
  5. Select an existing layout or write a (New) layout.
    • The (Default) layout uses the markup of the web part’s source file.
  6. Click OK.

The appearance and content of the web part instance changes according to the assigned layout.

Setting default layouts for inherited web parts

You can prepare specialized versions of web parts that use a specific layout by default:

  1. Open the Web parts application.
  2. Create an inherited web part based on the web part you want to modify.
  3. On the Layouts tab of the inherited web part, define the required custom layout.
  4. Enable the Is default option of the given layout.
    • An inherited web part cannot have more than one default layout at a given time.
    • If there is no default layout, the inherited web part uses the default markup of the parent web part.

The system automatically assigns the selected layout to new instances of the inherited web part. You can choose a different layout for existing web part instances at any time.

Adding CSS styles to web part layouts

Web part layouts allow you to directly define any CSS classes used within the layout code.

Requirement: Enable the Allow CSS from components setting in Settings -> System -> Performance.

  1. Click Add CSS styles below the layout’s code. The CSS styles editor appears.
  2. Enter the definitions of the required CSS classes.
  3. Click Save.
  4. If the styles require any files (such as images), you can upload them on the web part’s Theme tab.

All pages that contain a web part with the given layout automatically load the specified styles (in addition to the website or page‑specific stylesheet).

See also: Adding CSS to page components