Codeblock error in design tab but not the front-end of website

Lennard van Diggelen asked on September 21, 2016 15:43

Hi!

I load the headerimage like this which works fine on the front-end:

style='background-image: url("~/getattachment/<%= CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("PageHeaderImage") %>/<%= CMS.DocumentEngine.DocumentContext.CurrentDocument.DocumentName %>.aspx")'

But on the design tab of the page app I get this error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

What's the proper way to achieve a result that works both ways?

Thanks!

Recent Answers


Brenden Kehren answered on September 21, 2016 16:01

Use a repeater with a transformation OR bind the whole page and use <%# %> instead. I'm sure there are others but it's pretty easy to use a repeater vs. inline code.

0 votesVote for this answer Mark as a Correct answer

Lennard van Diggelen answered on September 21, 2016 16:12

Hi Brenden,

What do you mean with 'bind the whole page'? If I replace it with <%# %> I get background-image: url("/getattachment//.aspx"). The strings are missing.

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on September 21, 2016 19:07 (last edited on December 10, 2019 02:30)

Could you please share more info? Like where you place that code, etc.? Anyway, code blocks are not supported in many locations, but you can use macros instead (HTML layout, Text/XML transformations, etc.) and then the code would look like this:

style='background-image: url("~/getattachment/{% CurrentDocument.GetValue("PageHeaderImage") |(identity)GlobalAdministrator%}.aspx")'
0 votesVote for this answer Mark as a Correct answer

Lennard van Diggelen answered on September 22, 2016 09:25

Hi!

Thank you for your feedback!

I've placed them in a template ascx file. But because it's an ascx file we cannot use those tokens like that. Is there a better approach?

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on September 22, 2016 09:41

Hi,
Have you tried using this workaround?
The code blocks are not supported in Kentico so if possible, I would try avoiding them.

0 votesVote for this answer Mark as a Correct answer

Lennard van Diggelen answered on September 22, 2016 10:01 (last edited on September 22, 2016 10:01)

Hi,

I've indeed tried that but it always returns this:

The name '[elementID]' does not exist in the current context

(elementId is not the actual id ;))

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.