2007年8月14日 星期二

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

將 VS2003 的asp.net 應用程式migrate 到 VS2005 web application 後,有些原有可以執行的網頁,變的不能執行了 錯誤如下 The Controls collection cannot be modified because the control contains code blocks (i.e. ). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. ).Source Error: Line 157: // Line 158: InitializeComponent(); Line 159: base.OnInit(e); Line 160: }Source File: C:\Projects\BankPro\EI\Web\Web.V1.0.7\EInvoiceSolution\EInvoice2\Profile\CompanyEdit.aspx.cs Line: 159 為什麼呢?MS宣傳的太好了,但現實往往是殘酷的。 原來我在網頁中的client javascript ,使用了 <%=%>,如下 function setField() { var strUrl = "InvoiceDisplayFieldSetting.aspx?oid=<%=OID%>"; window.showModalDialog(strUrl,"setField", "dialogWidth:400px;dialogHeight:630px;center:1;scroll:0;help:0;status:0;resizable=0"); } 在 html 中的不會有問題, 但使用了<head runat="server"> 後, client side javacript 就不能使用 解法是override OnInit() 如下 override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); Page.DataBind(); } 然後在 client side javacript ,改用 <%# OID%> 大功告成

沒有留言:

Share with Facebook