我試圖建立一個簡單的用戶控件,它是一個滑塊。 當我將AjaxToolkit SliderExtender添加到用戶控件時,出現此錯誤(*&$#()@#錯誤: javascript
Server Error in '/' 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: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`).] System.Web.UI.ControlCollection.Add(Control child) +8677431 AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:293 AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:306 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
我嘗試在用戶控件中放置一個佔位符,並以編程方式將文本框和滑塊擴展程序添加到該佔位符,但仍然出現錯誤。 css
這是簡單的代碼: java
<table cellpadding="0" cellspacing="0" style="width:100%"> <tbody> <tr> <td></td> <td> <asp:Label ID="lblMaxValue" runat="server" Text="Maximum" CssClass="float_right" /> <asp:Label ID="lblMinValue" runat="server" Text="Minimum" /> </td> </tr> <tr> <td style="width:60%;"> <asp:CheckBox ID="chkOn" runat="server" /> <asp:Label ID="lblPrefix" runat="server" />: <asp:Label ID="lblSliderValue" runat="server" /> <asp:Label ID="lblSuffix" runat="server" /> </td> <td style="text-align:right;width:40%;"> <asp:TextBox ID="txtSlider" runat="server" Text="50" style="display:none;" /> <ajaxToolkit:SliderExtender ID="seSlider" runat="server" BehaviorID="seSlider" TargetControlID="txtSlider" BoundControlID="lblSliderValue" Orientation="Horizontal" EnableHandleAnimation="true" Length="200" Minimum="0" Maximum="100" Steps="1" /> </td> </tr> </tbody> </table>
問題是什麼? web
首先,使用<%#而不是<%=來啓動代碼塊: ajax
<head id="head1" runat="server"> <title>My Page</title> <link href="css/common.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script> </head>
這會將代碼塊從Response.Write代碼塊更改成數據綁定表達式。
因爲<%# ... %>
數據綁定表達式不是代碼塊,所以CLR不會抱怨。 而後,在母版頁代碼中,添加如下內容: 編程
protected void Page_Load(object sender, EventArgs e) { Page.Header.DataBind(); }
我遇到了一樣的問題,但與JavaScript沒有任何關係。 考慮如下代碼: 服務器
<input id="hdnTest" type="hidden" value='<%= hdnValue %>' /> <asp:PlaceHolder ID="phWrapper" runat="server"></asp:PlaceHolder> <asp:PlaceHolder ID="phContent" runat="server" Visible="false"> <b>test content</b> </asp:PlaceHolder>
在這種狀況下,即便PlaceHolders沒有任何有害的代碼塊,您也會遇到相同的錯誤,它的發生是因爲非服務器控件hdnTest使用代碼塊。 app
只需將runat = server添加到hdnTest,便可解決問題。 ide
我也面臨一樣的問題。 我找到了如下解決方案。 ui
解決方案1:我將腳本標籤保留在體內。
<body> <form> . . . . </form> <script type="text/javascript" src="<%= My.Working.Common.Util.GetSiteLocation()%>Scripts/Common.js"></script> </body>
如今,有關標籤的衝突將解決。
解決方案2:
咱們也能夠解決上述解決方案之一,例如用<%#代替<%=代替代碼塊,可是問題是它只會給出相對路徑 。 若是您想要真正的絕對路徑 ,它將行不通。
解決方案1對我有效。 接下來是您的選擇。
「 <%#」數據綁定技術將沒法直接在<head>標記中的<link>標記內使用:
<head runat="server"> <link rel="stylesheet" type="text/css" href="css/style.css?v=<%# My.Constants.CSS_VERSION %>" /> </head>
上面的代碼將評估爲
<head> <link rel="stylesheet" type="text/css" href="css/style.css?v=<%# My.Constants.CSS_VERSION %>" /> </head>
相反,您應該執行如下操做(請注意其中的兩個雙引號):
<head runat="server"> <link rel="stylesheet" type="text/css" href="css/style.css?v=<%# "" + My.Constants.CSS_VERSION %>" /> </head>
而後您將得到理想的結果:
<head> <link rel="stylesheet" type="text/css" href="css/style.css?v=1.5" /> </head>
將Java腳本代碼保留在body標籤內
<body> <script type="text/javascript"> </script> </body>