Search code examples
kentico

How to access the values of web part properties in the layout?


In the Kentico Documentation I only found the info as below...

enter image description here

Is there any way to access the properties value in the layout as show below? I tried to use macro but it didn't work.

enter image description here

I just want to display the properties value in my custom layout. Any methods other than access through the code? I'm using portal engine, I have no idea how to access the code behind...


Solution

  • The layout is ASCX, so you won't be able to use macros as per your example.

    • If you just need the value, you can use the GetValue method. There's also GetStringValue, if the type of your property is a string
        <% GetStringValue("MyPropertiesValue1", string.Empty); %>
    
    • If you need to render the value, you will need to call Page.DataBind() and use a data binding expression. Your layout would look something like this:
        <%# GetStringValue("MyPropertiesValue1", string.Empty) %>
        <%# GetStringValue("MyPropertiesValue2", string.Empty) %>
    
        <script runat="server">
            protected void Page_Load(object sender, EventArgs e)
            {
                Page.DataBind();
            }
        </script>
    


    None if this is really elegant, so you might want to reconsider your approach.