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 bom开发者_如何学编程bs out at this stage, giving the following error:
"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). "
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>
精彩评论