Search code examples
ascxkentico

Kentico 7 create content placeholder in Portal Master to use in ASCX in inherited page


Working in Kentico 7 on an Ad-Hoc page that inherits from Portal Master. I want to insert some literal script or code right before the </body> tag in the rendered ad-hoc page.

I thought I'd have to do this by editing the portal master and adding the following:

<cms:CMSPagePlaceholder ID="plcBodyEnd" runat="server">
   <LayoutTemplate>
   </LayoutTemplate>
</cms:CMSPagePlaceholder>

and then in the layout of the Ad-Hoc page do this:

<cms:CMSContent runat="server" id="cntLeft" PagePlaceholderID="plcBodyEnd">
<script type="text/javascript"> 
ProviderConnections.Transparency.initializeWidget({ }); 
</script> 
</cms:CMSContent>

This worked fine until I went to the design tab on the Ad-Hoc page, where I got the following error:

Object reference not set to an instance of an object.

I don't want to register script blocks. I just want to put text in the Ad-Hoc page that goes there before the </body> tag, which is controlled by Portal Master.

What am I doing wrong?


Solution

  • I'm not 100% sure what you are trying to achieve. Giving an example or attaching a screenshot would be very helpful.

    Here are the ways of attaching JavaScript in Kentico:

    Through portal engine:

    • Use JavaScript web part - that gives you an option of choosing where the script should be located

    JavaScript web part

    Programmatically from code-behind:

    • Use CMS.Helpers.ScriptHelper API (wrapper around ASP.NET's ClientScriptManager)
      • ScriptHelper.RegisterStartupScript() to put the script at the end of the page
      • ScriptHelper.RegisterClientScriptBlock() to put the script before page's elements
    • The difference between the two is well explained here.

    Programmatically from ASPX markup:

    • Put your <script> block to a desired location in your .aspx / .ascx files
    • Evaluate a code-behind variable containing script

      <asp:Button ID="btnOK" runat="server" Text="OK" /> <script type="text/javascript"> <%= fieldWithActualScript %> </script>