The Controls collection cannot be modified because the control

The Controls collection cannot be modified because the control

am 16.04.2008 16:13:41 von Radu

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


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" %>
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Welcome






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


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


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


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

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

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







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.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(strLa ng)
System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo(strLang)
Else 'Default to language "en"
UICulture = "EN-CA"
Culture = "EN-CA"
Session("Language") = "EN-CA"
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-C A")
System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo("en-CA")
End If
End If
End Sub
End Class

Re: The Controls collection cannot be modified because the control

am 16.04.2008 16:59:51 von Radu

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" %>
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Welcome







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


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


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




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

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






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.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture(strLa ng)
System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo(strLang)
Else 'Default to language "en"
UICulture = "EN-CA"
Culture = "EN-CA"
Session("Language") = "EN-CA"
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-C A")
System.Threading.Thread.CurrentThread.CurrentUICulture = 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("")
ClientScript.RegisterClientScriptBlock(Me.GetType, "Alert",
objStringBuilder.ToString)
End Sub
End Class
------------------------------------------------------------ --------------------------------------------------

Thanks for reading.
Alex.