The Controls collection cannot be modified because the controlcontains code blocks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Radu

    #1

    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(stringToTr im) {
    return stringToTrim.re place(/^\s+|\s+$/g,"");
    }

    function RecipientAddres s_SetToBlank(){
    var t=trim(event.sr cElement.value)
    if (t == s)
    {
    event.srcElemen t.value = "";
    }
    }

    function RecipientAddres s_SetToDefault( ){
    var t=trim(event.sr cElement.value)
    if (t == "")
    {
    event.srcElemen t.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="Recipi entAddress_SetT oBlank()"
    onblur="Recipie ntAddress_SetTo Default()"

    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 = GetGlobalResour ceObject("Score cards", "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.C ontrolCollectio n.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.as px":
    HTML:

    -----------------------------------------------------------------------------------------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup ="false"
    CodeBehind="Wel come.aspx.vb" Inherits="Temp2 .Welcome" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>Welcom e</title>
    <script>
    var s=trim('<%= jsTextAddress %>');

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

    function RecipientAddres s_SetToBlank(){
    var t=trim(event.sr cElement.value)
    if (t == s)
    {
    event.srcElemen t.value = "";
    }
    }

    function RecipientAddres s_SetToDefault( ){
    var t=trim(event.sr cElement.value)
    if (t == "")
    {
    event.srcElemen t.value = s;
    }
    }
    </script>
    </head>

    <body>
    <form id="frmWelcome " runat="server">
    <div>
    <textarea
    title="<%$ Resources:Score cards, String2 %>"
    rows="5"
    id="txtRecipien t_Address"
    style="width:10 0%;"
    onfocus="Recipi entAddress_SetT oBlank()"
    onblur="Recipie ntAddress_SetTo Default()"
    runat="server">
    <%= jsTextAddress %>
    </textarea>

    <asp:RequiredFi eldValidator
    ID="req_txtReci pient_Address1"
    Display="Dynami c"
    ControlToValida te="txtRecipien t_Address"
    Text="*"
    InitialValue="< %= jsTextAddress %>"
    ErrorMessage="< %$ Resources:Score cards, String5 %>"
    runat="server">
    </asp:RequiredFie ldValidator>

    <asp:RequiredFi eldValidator
    ID="req_txtReci pient_Address2"
    Display="Dynami c"
    ControlToValida te="txtRecipien t_Address"
    Text="*"
    ErrorMessage="< %$ Resources:Score cards, String5 %>"
    runat="server">
    </asp:RequiredFie ldValidator>

    <asp:Validation Summary
    ID="validSummar y"
    ShowSummary="fa lse"
    DisplayMode="Bu lletList"
    HeaderText="<%$ Resources:Score cards, String4 %>"
    ShowMessageBox= "true"
    runat="server"
    />

    <input
    type="text"
    id="txtRecipien tSummary"
    value="<%$ Resources:Score cards, String6%"
    runat="server"
    title="<%$ Resources:Score cards, String6%"
    style="width:10 0%"
    />

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



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

    Public jsTextAddress As String

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

    jsTextAddress = GetGlobalResour ceObject("Score cards", "String1")
    End Sub

    Protected Overrides Sub InitializeCultu re()

    Dim strLang As String

    strLang = Context.Request .Params("Langua ge")
    If Not String.IsNullOr Empty(strLang) Then
    If UCase(strLang) = "FR-CA" Then
    UICulture = strLang
    Culture = strLang
    Session("Langua ge") = "FR-CA"
    System.Threadin g.Thread.Curren tThread.Current Culture =
    System.Globaliz ation.CultureIn fo.CreateSpecif icCulture(strLa ng)
    System.Threadin g.Thread.Curren tThread.Current UICulture = New
    System.Globaliz ation.CultureIn fo(strLang)
    Else 'Default to language "en"
    UICulture = "EN-CA"
    Culture = "EN-CA"
    Session("Langua ge") = "EN-CA"
    System.Threadin g.Thread.Curren tThread.Current Culture =
    System.Globaliz ation.CultureIn fo.CreateSpecif icCulture("en-CA")
    System.Threadin g.Thread.Curren tThread.Current UICulture = New
    System.Globaliz ation.CultureIn fo("en-CA")
    End If
    End If
    End Sub
    End Class
  • Radu

    #2
    Re: The Controls collection cannot be modified because the controlcontains code blocks

    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_A ddress" from the HTML and add it to the code-behind,
    like so:


    HTML:
    --------------------------------------------------------------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup ="false"
    CodeBehind="Wel come.aspx.vb" Inherits="Temp2 .Welcome" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>Welcom e</title>
    <script>
    var s=trim('<%= jsTextAddress %>');

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

    function RecipientAddres s_SetToBlank(){
    var t=trim(event.sr cElement.value)
    if (t == s)
    {
    event.srcElemen t.value = "";
    }
    }

    function RecipientAddres s_SetToDefault( ){
    var t=trim(event.sr cElement.value)
    if (t == "")
    {
    event.srcElemen t.value = s;
    }
    }
    </script>
    </head>


    <body>
    <form id="frmWelcome " runat="server">
    <div>
    <textarea
    title="<%$ Resources:Score cards, String2 %>"
    rows="5"
    id="txtRecipien t_Address"
    style="width:10 0%;"
    onfocus="Recipi entAddress_SetT oBlank()"
    onblur="Recipie ntAddress_SetTo Default()"
    runat="server">
    </textarea>

    <asp:RequiredFi eldValidator
    ID="req_txtReci pient_Address1"
    Display="Dynami c"
    ControlToValida te="txtRecipien t_Address"
    Text="*"
    InitialValue="< %= jsTextAddress %>"
    ErrorMessage="< %$ Resources:Score cards, String5 %>"
    runat="server">
    </asp:RequiredFie ldValidator>

    <input
    type="text"
    id="txtRecipien tSummary"
    value="<%$ Resources:Score cards, String6%"
    runat="server"
    title="<%$ Resources:Score cards, String6%"
    style="width:10 0%"
    />

    <br />

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

    <asp:Validation Summary
    ID="validSummar y"
    ShowSummary="fa lse"
    DisplayMode="Bu lletList"
    HeaderText="<%$ Resources:Score cards, String4 %>"
    ShowMessageBox= "true"
    runat="server"
    />
    </div>
    </form>
    </body>
    </html>


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

    Public jsTextAddress As String

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

    jsTextAddress = Trim(GetGlobalR esourceObject(" Scorecards",
    "String1"))
    If Not Page.IsPostBack Then
    txtRecipient_Ad dress.Value = jsTextAddress
    End If
    End Sub

    Protected Overrides Sub InitializeCultu re()

    Dim strLang As String

    strLang = Context.Request .Params("Langua ge")
    If Not String.IsNullOr Empty(strLang) Then
    If UCase(strLang) = "FR-CA" Then
    UICulture = strLang
    Culture = strLang
    Session("Langua ge") = "FR-CA"
    System.Threadin g.Thread.Curren tThread.Current Culture =
    System.Globaliz ation.CultureIn fo.CreateSpecif icCulture(strLa ng)
    System.Threadin g.Thread.Curren tThread.Current UICulture = New
    System.Globaliz ation.CultureIn fo(strLang)
    Else 'Default to language "en"
    UICulture = "EN-CA"
    Culture = "EN-CA"
    Session("Langua ge") = "EN-CA"
    System.Threadin g.Thread.Curren tThread.Current Culture =
    System.Globaliz ation.CultureIn fo.CreateSpecif icCulture("en-CA")
    System.Threadin g.Thread.Curren tThread.Current UICulture = New
    System.Globaliz ation.CultureIn fo("en-CA")
    End If
    End If
    End Sub

    Private Sub cmdOk_Click(ByV al sender As Object, ByVal e As
    System.EventArg s) Handles cmdOk.Click

    Dim objStringBuilde r As StringBuilder = New StringBuilder
    Dim strMsg As String

    objStringBuilde r.Append("<scri pt language='javas cript'>")

    strMsg = "The values of the controls on this page are:"
    strMsg = strMsg & "\n" & txtRecipient_Ad dress.Value
    strMsg = strMsg & "\n" & txtRecipientSum mary.Value
    objStringBuilde r.Append("alert ( """ + strMsg + """ );")

    objStringBuilde r.Append("</script>")
    ClientScript.Re gisterClientScr iptBlock(Me.Get Type, "Alert",
    objStringBuilde r.ToString)
    End Sub
    End Class
    --------------------------------------------------------------------------------------------------------------

    Thanks for reading.
    Alex.

    Comment

    Working...