This is a migrated thread and some comments may be shown as answers.
Explain the "Code Block in Body Element" Restriction?
1 Answer 84 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Robert asked on 08 Jul 2008, 05:36 PM

Is there some technical explanation available for the apparent restriction upon the use of "code blocks" within pages containing the RadUpload control?

I have a VB.NET application in development using the RadUpload control. The application uses a Master Page design, with the usual Content Placeholder control to render in variable "child-page" content. One of the child-pages contains a RadUpload control.

I needed to add some client-side javascript for other child-page processes completely unrelated to the RadUpload control. That client-side javascript required the use of a .NET "code block" (<%= … %>) to render-in a value from the page's code-behind script.

The client-side javascript block is a normal script block, placed in the Master Page document HEAD. And everything about it works fine across all child-pages, until I render-in the child-page containing the RadUpload control.

At that point I get an error:
"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)"

A little poking around your site revealed, in a completely unrelated area, the restriction:
"The <%= .. %> codeblock must be placed inside the BODY element of the page."

The example noted was capturing the RadUpload control's ClientID, so I figured it didn't apply to me, since I wasn't messing with the RadUpload control at all.

And yet, just as noted, the page with the RadUpload control will not render until I place my script block (with that "code block" inside) within the Master page's BODY tag.

Now, I'm no stickler for "Script in the Head" and don't mind moving the script block. But I'd like to understand what's going on.

There are places in the final design where I will have at least one other 3rd Party control in my page, with its own clot of client-side script, collocated with the RadUpload control. And, like with the RadUpload control, I don't have a lot of influence over how that control and its scripting nestles into the rendered page.

So any explanation for the "code blocks in the body, please" requirement would be appreciated.


1 Answer, 1 is accepted

Sort by
Erjan Gavalji
Telerik team
answered on 09 Jul 2008, 10:37 AM
Hi Robert,

The problem comes from the skinning model of RadControls and their XHTML compatibility. A requirement for a XHTML compliant page is that the <link> tags pointing to the StyleSheet files the page uses must be located in the page <head> tag. RadControls (in this particular case - RadUpload) search a reference to the page head tag and add a HtmlGenericControl (representing the the <link> tag) there.

There is a restriction from the ASP.NET engine, that a control, which Controls collection gets modified cannot contain code blocks, hence the error.

The workaround is to just add the script in a <telerik:RadCodeBlock> control, which eliminates this problem.

I hope this helps.

Kind regards,
Erjan Gavalji
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Asked by
Top achievements
Rank 1
Answers by
Erjan Gavalji
Telerik team
Share this question