Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

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


Q. Is there another way to code this in ASP because it gives me a Code Block error residing in the HEAD of our Master Page?

<script type="text/javascript" src="<%= Page.ResolveClientUrl("/scripts/JScript.js") %>"></script>

Error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)?
Avatar of crisco96
crisco96
Flag of United States of America image

Replace:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("/scripts/JScript.js") %>"></script>

Open in new window


with

<asp:Literal runat="server" id="ScriptReference" />

Open in new window


Then in your code behind do:
protected void Page_Load(object sender, EventArgs e)
{
     ScriptReference.Text = "<script type='text/javascript' src=' + Page.ResolveClientUrl("/scripts/JScript.js") "'></script>";
}

Open in new window

Hi

If you place a placeholder in the head section like this
<head>
<asp:PlaceHolder ID="MyScriptPH" runat="server" />
</head>

Open in new window


Then in the Page_Load section (or where ever you want it to be) add this

Dim objPH As New PlaceHolder()

objPH = Master.FindControl("MyScriptPH")

Dim objScript As New HtmlGenericControl("script")

objScript.Attributes.Add("type", "text/javascript")
objScript.Attributes.Add("src", Page.ResolveClientURL("/scripts/JScript.js"))

objPH.Controls.Add(objScript)

Open in new window


Jawa29
Avatar of pointeman

ASKER

crisco96:

will this work? <script language="javascript" type="text/javascript" src="scripts/JScript.js" />

Error: Too many characters in character literal

ScriptReference.Text = "<script type='text/javascript' src=' + Page.ResolveClientUrl("/scriptz/JScript.js") "'></script>";
ASKER CERTIFIED SOLUTION
Avatar of crisco96
crisco96
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very good, thanks...

Q.will this work? <script language="javascript" type="text/javascript" src="scripts/JScript.js" />

I found is else where on the web.