简体   繁体   中英

Modifying controls collection error in VB.net

I need a function to be performed on the page load, which uses a stored session variable. I have added the following to my <body> tag.

<body onload="doSomething(event,'<%= Session("StartTime") %>')>

This does work. However, it is causing a problem elsewhere, when I try to add a control to my controls collection:

dim myPanel= New Panel
...
Me.Controls.Add(myPanel)

It bombs out at this stage, giving the following error:

"The Controls collection cannot be modified because the control contains code blocks (ie <% ... %>). "

I've tried the suggestion of using <%# ... %> instead of <%= .. .%> , but this prevents the session variable being found- it is just blank.

Or,

<body onload="doSomething(event,'<asp:PlaceHolder id="starttimePlaceholder" runat="server"</asp:Placeholder>')>

Then, on the server side to populate it:

starttimePlaceholder.Controls.Add(New LiteralControl(Session("StartTime")))

I found a solution that worked

I forced my script to run on a server-control.

<body ms_positioning="GridLayout" onload="callFromDiv(event);">

         <div runat="server" ID="serverDiv">
            <script type="text/javascript">
                  //This function must be placed in a separate div, since placing it directly 
                  //in body tag prevents new controls being added later
                  function callFromDiv(e){
                      doSomething(e,'<%= Session("StartTime") %>')
                  }  
            </script>
        </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM