我试图建立一个简单的用户控件,它是一个滑块。 当我将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>