473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The Controls collection cannot be modified because the controlcontains code blocks

Hi. I am trying to localize a page AND simulate the AJAX watermarks in
it, i.e. the textbox contains the text "Enter your name", or "Entrez
votre nom", for instance, then on click, delete this text, allow
entry, but on deletion of the inputted text show again "Enter your
name", or "Entrez votre nom" depending on the UICulture.

Everything worked great UNTIL I have added a button which posts back.
Now on postback I have the error in the title.

This is how it is supposed to work. The page contains the script
<script>
var s=trim('<%= jsTextAddress %>');

function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function RecipientAddress_SetToBlank(){
var t=trim(event.srcElement.value)
if (t == s)
{
event.srcElement.value = "";
}
}

function RecipientAddress_SetToDefault(){
var t=trim(event.srcElement.value)
if (t == "")
{
event.srcElement.value = s;
}
}
</script>

The "trim" function is necessary because (first stupid unexplained
thing) the clean strings entered in the resource files come back with
lots of Tabs, CRLFs, and spaces (!!!!!!!!?????)

The textarea in question has the HTML
onfocus="RecipientAddress_SetToBlank()"
onblur="RecipientAddress_SetToDefault()"

The whole thing is supposed to compare the existing string in the
textbox control with the "LOCALIZED" default contents, that is "Enter
your name" or "Entrez votre nom". That's why I need to use the
variable <%= jsTextAddress %instead of comparing it directly with
the "Enter your name" string.

In code behind, in Page Load, I have
jsTextAddress = GetGlobalResourceObject("Scorecards", "String1")

It all worked great - on load the text area showed either "Enter your
name" or "Entrez votre nom", when cliking in the textarea it became
blank waiting for input, if input than the input stays, if the input
is deleted then the contents reverts to either "Enter your name" or
"Entrez votre nom" depending on the culture - exactly as a watermarked
text control is supposed to work.

However, as soon as I have added a postback control on the page, I
have started to receive the error "The Controls collection cannot be
modified because the control contains code blocks" which happens on
System.Web.UI.ControlCollection.Add(Control child)

How can I fix this please ?
Thanks a lot. Alex.

PS. This is the code for quick pasting in a form named "Welcome.aspx":
HTML:

-----------------------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="Welcome.aspx.vb" Inherits="Temp2.Welcome" %>
<!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 id="Head1" runat="server">
<title>Welcome</title>
<script>
var s=trim('<%= jsTextAddress %>');

function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function RecipientAddress_SetToBlank(){
var t=trim(event.srcElement.value)
if (t == s)
{
event.srcElement.value = "";
}
}

function RecipientAddress_SetToDefault(){
var t=trim(event.srcElement.value)
if (t == "")
{
event.srcElement.value = s;
}
}
</script>
</head>

<body>
<form id="frmWelcome" runat="server">
<div>
<textarea
title="<%$ Resources:Scorecards, String2 %>"
rows="5"
id="txtRecipient_Address"
style="width:100%;"
onfocus="RecipientAddress_SetToBlank()"
onblur="RecipientAddress_SetToDefault()"
runat="server">
<%= jsTextAddress %>
</textarea>

<asp:RequiredFieldValidator
ID="req_txtRecipient_Address1"
Display="Dynamic"
ControlToValidate="txtRecipient_Address"
Text="*"
InitialValue="<%= jsTextAddress %>"
ErrorMessage="<%$ Resources:Scorecards, String5 %>"
runat="server">
</asp:RequiredFieldValidator>

<asp:RequiredFieldValidator
ID="req_txtRecipient_Address2"
Display="Dynamic"
ControlToValidate="txtRecipient_Address"
Text="*"
ErrorMessage="<%$ Resources:Scorecards, String5 %>"
runat="server">
</asp:RequiredFieldValidator>

<asp:ValidationSummary
ID="validSummary"
ShowSummary="false"
DisplayMode="BulletList"
HeaderText="<%$ Resources:Scorecards, String4 %>"
ShowMessageBox="true"
runat="server"
/>

<input
type="text"
id="txtRecipientSummary"
value="<%$ Resources:Scorecards, String6%"
runat="server"
title="<%$ Resources:Scorecards, String6%"
style="width:100%"
/>

<asp:Button
Width="150"
AccessKey="N"
Text="<%$ Resources:Scorecards, String7 %>"
ID="cmdOk"
runat="server"
/>
</div>
</form>
</body>
</html>

Code-behind:
-----------------------------------------------------------------------------------------------------------------------------------------
Public Partial Class Welcome
Inherits System.Web.UI.Page

Public jsTextAddress As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

jsTextAddress = GetGlobalResourceObject("Scorecards", "String1")
End Sub

Protected Overrides Sub InitializeCulture()

Dim strLang As String

strLang = Context.Request.Params("Language")
If Not String.IsNullOrEmpty(strLang) Then
If UCase(strLang) = "FR-CA" Then
UICulture = strLang
Culture = strLang
Session("Language") = "FR-CA"
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(strLang)
System.Threading.Thread.CurrentThread.CurrentUICul ture = New
System.Globalization.CultureInfo(strLang)
Else 'Default to language "en"
UICulture = "EN-CA"
Culture = "EN-CA"
Session("Language") = "EN-CA"
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture("en-CA")
System.Threading.Thread.CurrentThread.CurrentUICul ture = New
System.Globalization.CultureInfo("en-CA")
End If
End If
End Sub
End Class
Jun 27 '08 #1
1 1648
I have "solved" the problem... I don't understand why it's solved, the
only thing I did was to take out the inital population of the textbox
"txtRecipient_Address" from the HTML and add it to the code-behind,
like so:
HTML:
--------------------------------------------------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="Welcome.aspx.vb" Inherits="Temp2.Welcome" %>
<!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 id="Head1" runat="server">
<title>Welcome</title>
<script>
var s=trim('<%= jsTextAddress %>');

function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function RecipientAddress_SetToBlank(){
var t=trim(event.srcElement.value)
if (t == s)
{
event.srcElement.value = "";
}
}

function RecipientAddress_SetToDefault(){
var t=trim(event.srcElement.value)
if (t == "")
{
event.srcElement.value = s;
}
}
</script>
</head>
<body>
<form id="frmWelcome" runat="server">
<div>
<textarea
title="<%$ Resources:Scorecards, String2 %>"
rows="5"
id="txtRecipient_Address"
style="width:100%;"
onfocus="RecipientAddress_SetToBlank()"
onblur="RecipientAddress_SetToDefault()"
runat="server">
</textarea>

<asp:RequiredFieldValidator
ID="req_txtRecipient_Address1"
Display="Dynamic"
ControlToValidate="txtRecipient_Address"
Text="*"
InitialValue="<%= jsTextAddress %>"
ErrorMessage="<%$ Resources:Scorecards, String5 %>"
runat="server">
</asp:RequiredFieldValidator>

<input
type="text"
id="txtRecipientSummary"
value="<%$ Resources:Scorecards, String6%"
runat="server"
title="<%$ Resources:Scorecards, String6%"
style="width:100%"
/>

<br />

<asp:Button
Width="150"
AccessKey="N"
Text="<%$ Resources:Scorecards, String7 %>"
ID="cmdOk"
runat="server"
/>

<asp:ValidationSummary
ID="validSummary"
ShowSummary="false"
DisplayMode="BulletList"
HeaderText="<%$ Resources:Scorecards, String4 %>"
ShowMessageBox="true"
runat="server"
/>
</div>
</form>
</body>
</html>
Code behind:
--------------------------------------------------------------------------------------------------------------
Public Partial Class Welcome
Inherits System.Web.UI.Page

Public jsTextAddress As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

jsTextAddress = Trim(GetGlobalResourceObject("Scorecards",
"String1"))
If Not Page.IsPostBack Then
txtRecipient_Address.Value = jsTextAddress
End If
End Sub

Protected Overrides Sub InitializeCulture()

Dim strLang As String

strLang = Context.Request.Params("Language")
If Not String.IsNullOrEmpty(strLang) Then
If UCase(strLang) = "FR-CA" Then
UICulture = strLang
Culture = strLang
Session("Language") = "FR-CA"
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture(strLang)
System.Threading.Thread.CurrentThread.CurrentUICul ture = New
System.Globalization.CultureInfo(strLang)
Else 'Default to language "en"
UICulture = "EN-CA"
Culture = "EN-CA"
Session("Language") = "EN-CA"
System.Threading.Thread.CurrentThread.CurrentCultu re =
System.Globalization.CultureInfo.CreateSpecificCul ture("en-CA")
System.Threading.Thread.CurrentThread.CurrentUICul ture = New
System.Globalization.CultureInfo("en-CA")
End If
End If
End Sub

Private Sub cmdOk_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdOk.Click

Dim objStringBuilder As StringBuilder = New StringBuilder
Dim strMsg As String

objStringBuilder.Append("<script language='javascript'>")

strMsg = "The values of the controls on this page are:"
strMsg = strMsg & "\n" & txtRecipient_Address.Value
strMsg = strMsg & "\n" & txtRecipientSummary.Value
objStringBuilder.Append("alert( """ + strMsg + """ );")

objStringBuilder.Append("</script>")
ClientScript.RegisterClientScriptBlock(Me.GetType, "Alert",
objStringBuilder.ToString)
End Sub
End Class
--------------------------------------------------------------------------------------------------------------

Thanks for reading.
Alex.
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jeff Dockman | last post by:
Background: We needed the ability to create a templated web site that may change in layout from installation to installation. To accomplish this, we created a base Page class that all pages in...
5
by: Jeremy Ames | last post by:
I have run into a major road block with my page development projects. I am trying to add a literal control to my asp.net page and I got an error stating that the control could not be added because...
2
by: Axel Dahmen | last post by:
HI, I want to dynamically add controls to a web page from within a common base class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The Controls collection cannot be modified...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
0
by: Dan | last post by:
Hi, I get this error: "Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). " when trying to create a table in the code-behind file like this: dim...
0
by: Zimmergren | last post by:
I know this has been up on googlegroups before, on several places. However, I havn't found a solution to this specific problem. I get this errormessage: "The Controls collection cannot be...
1
by: paul.hester | last post by:
Hi all, What is the recommended way to add child controls *around* existing child controls in the CreateChildControls method (e.g. table, row and cell controls for layout purposes)? Is the...
0
by: sonic | last post by:
I am trying to dynamically load a validator and must be missing something elementary here. I extended TextBox control to add some functionality to it. One new feature it contains is IsRequired...
2
by: CyberSoftHari | last post by:
I am adding dynamic controls in my page through a panel as figured below. pnlModel.ID = "pnlDyModel"; pnlModel.Controls.Add(lblReportTitle); pnlModel.Controls.Add(new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.