Dan*_*l P 365 c# asp.net user-controls
我正在尝试创建一个简单的用户控件,它是一个滑块.当我向用户控件添加AjaxToolkit SliderExtender时,我得到了这个(*&$#()@#error:
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
Run Code Online (Sandbox Code Playgroud)
我已经尝试在用户控件中放置一个占位符,并以编程方式将文本框和滑块扩展器添加到占位符,但仍然出现错误.
这是简单的代码:
<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>
Run Code Online (Sandbox Code Playgroud)
问题是什么?
Jal*_*aer 490
首先,使用<%#而不是<%=启动代码块:
<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>
Run Code Online (Sandbox Code Playgroud)
这会将代码块从Response.Write代码块更改为数据绑定表达式.
由于<%# ... %>数据绑定表达式不是代码块,因此CLR不会抱怨.然后在母版页的代码中添加以下内容:
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*ved 248
我刚刚遇到这个问题,但找到了另一个解决方案.
我发现用asp:PlaceHolder-tag包装代码块解决了这个问题.
<asp:PlaceHolder runat="server">
<meta name="ROBOTS" content="<%= this.ViewData["RobotsMeta"] %>" />
</asp:PlaceHolder>
Run Code Online (Sandbox Code Playgroud)
(我正在使用的CMS是从一些代码插入到头部区域,这些代码限制我添加自定义控制块以及各种信息,如元标记等,所以这是它对我有用的唯一方式.)
小智 53
我可以确认将带有<% %>标签的javascript 从头部移动到表单标签可以修复此错误
http://italez.wordpress.com/2010/06/22/ajaxcontroltoolkit-calendarextender-e-strana-eccezione/
小智 31
将javascript放在div标签下.
<div runat="server"> //div tag must have runat server
//Your Jave script code goes here....
</div>
Run Code Online (Sandbox Code Playgroud)
它会工作!!
小智 8
如果您在页面中使用脚本管理器,则可以执行相同的功能.你必须像这样注册脚本
<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Styles/javascript/jquery.min.js" />
</Scripts>
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
390511 次 |
| 最近记录: |