2011-03-31 18 views
1

我需要一個在頁面加載時執行的函數,它使用存儲的會話變量。我已將以下內容添加到我的<body>標記中。在VB.net中修改控件集合錯誤

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

這是行不通的。但是,在其他地方造成一個問題,當我試圖將一個控件添加到我的控件集合:

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

它彈了在這個階段,給了以下錯誤:

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

我已經嘗試了使用<%# ... %>而不是<%= .. .%>的建議,但是這會阻止找到會話變量 - 它只是空白。

回答

1

或者,

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

然後,在服務器端來填充它:

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

我發現a solution that worked

我強迫我的腳本在服務器控制運行。

<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> 
相關問題