Original http://www.cnblogs.com/jsonzheng/archive/2011/01/18/1938403.html
-
Control contains code blocks (that is, <%... %>), so the control set cannot be modified.
-
Note: An unhandled exception occurs during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
Exception details: System. Web. HttpException: Control contains code blocks (namely, <%... %>), so the control set cannot be modified.
-
I. Error example explanation:
-
<Asp: Panel ID = "p_Time" runat = "server">
<% = DateTime. Now. ToString ("HH-mm") %>
</Asp: Panel>
Label lb = new Label ();
Lb. Text = "Current Time ";
P_Time.Controls.Add (lb );
This error occurs when the preceding code is executed. Because the p_Time control contains a code block (namely <%... %>), the control set cannot be modified. Therefore, an error occurs when the statement p_Time.Controls.Add (lb) is executed.
-
2. Common Errors and Solutions
-
Generally, in the javascript code of the header of the page code, <% = this. control. clientID %> to obtain the Server Control identifier (the Code is as follows). When the topic and other functions are applied, the server script dynamically adds the control to the Header, causing an error .?
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript"> function showTime() { var now = new Date(); var lbTime = document.getElementById('<%= this.lb_Time.ClientID %>'); lbTime.innerHTML = now.getHours() + ":" + now.getMinutes(); } </script> </head> <body> <asp:Label ID="lb_Time" runat="server" Text="lb_Time"></asp:Label> <input id="btn_Show" type="button" value="button" onclick="return showTime()" /> </body></html> |
The solution is as follows:
-
1. Place javascript in the body.
-
2. Declare a custom control to replace the code block (that is, <%... %>)
-
Control Code :?
public class ControlInfoWriter : Control{ private string writerControlID; private string writerProperty; public string WriterControlID { get { return writerControlID; } set { writerControlID = value; } } public string WriterProperty { get { return writerProperty; } set { writerProperty = value; } } protected override void Render(System.Web.UI.HtmlTextWriter writer) { object writerControl = this.Parent.FindControl(writerControlID);// Search for the control to be output Type t = writerControl.GetType();// Obtain the control type PropertyInfo pi = t.GetProperty(writerProperty);// Obtain the attribute to be output string value = pi.GetValue(writerControl, null).ToString();// Obtain the property value of the control. writer.Write(value); base.Render(writer); }} |
The javascript modification in head is as follows :?
<script type="text/javascript">function showTime(){ var now = new Date(); var lbTime = document.getElementById('<CustomControl:ControlInfoWriter ID="controlInfoWriter" WriterControlID="lb_Time" WriterProperty="ClientID" runat="server" />'); lbTime.innerHTML = now.getHours() + ":" + now.getMinutes();}</script> |
Note: The mcontrol: ControlInfoWriter control must be registered in the page code.
Author: Zheng Ren
Source: http://jsonzheng.cnblogs.com
You are welcome to repost or share it, but be sure to declare the source of the article. If the article is helpful to you, I hope you can recommend it or follow it.