The Controls collection cannot be modified because the control contains code blocks
Sometimes when you write inline code inside Javascript tag or inside style sheet tag within the header of the page just like code 1, the yellow error page will explode in your face telling you that The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
There are two solutions to overcome this error.
Solution 1 :
Move the JavaScript tag to the bottom of the page, typically above the close form tag.
Solution 2 :
By registering the JavaScript code from code behind.
Protected void Page_Load(object sender, EventArgs e)
{
StringBuilder strScript = New StringBuilder();
strScript.Append("function fnTest()")
strScript.Append("{")
strScript.Append("var txt = document.getElementById('")
strScript.Append(Me.TextBox1.ClientID)
strScript.Append("');")
strScript.Append("alert(txt.value);")
strScript.Append("}")
Page.ClientScript.RegisterClientScriptBlock (Me.GetType, "MyScript", strScript.ToString, True)
}
Happy Coding...
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Inline Code inside JavaScript</title>
<script type="text/javascript" language="javascript">
function fnTest()
{
var txt = document.getElementById("<%= TextBox1.ClientID %>")
alert(txt.value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Inline Code inside JavaScript</title>
<script type="text/javascript" language="javascript">
function fnTest()
{
var txt = document.getElementById("<%= TextBox1.ClientID %>")
alert(txt.value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
There are two solutions to overcome this error.
Solution 1 :
Move the JavaScript tag to the bottom of the page, typically above the close form tag.
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Inline Code inside JavaScript</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script type="text/javascript" language="javascript">
function fnTest()
{
var txt = document.getElementById("<%= TextBox1.ClientID %>")
alert(txt.value);
}
</script>
</form>
</body>
</html>
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>Inline Code inside JavaScript</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script type="text/javascript" language="javascript">
function fnTest()
{
var txt = document.getElementById("<%= TextBox1.ClientID %>")
alert(txt.value);
}
</script>
</form>
</body>
</html>
Solution 2 :
By registering the JavaScript code from code behind.
Protected void Page_Load(object sender, EventArgs e)
{
StringBuilder strScript = New StringBuilder();
strScript.Append("function fnTest()")
strScript.Append("{")
strScript.Append("var txt = document.getElementById('")
strScript.Append(Me.TextBox1.ClientID)
strScript.Append("');")
strScript.Append("alert(txt.value);")
strScript.Append("}")
Page.ClientScript.RegisterClientScriptBlock (Me.GetType, "MyScript", strScript.ToString, True)
}
Happy Coding...
Labels: .net2.0, .net3.5, code behind, javascript, string, stringbuilder
Post a Comment