Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,

I want to use ajax control tool kit - combo box in my asp.net page,

For me this error is coming, when i'm using any of ajax control from ajaxControlToolKit. 

can anyone please help me to sort this.


code:
<asp:ComboBox ID="ComboBox1" runat="server" style="width: 300px;"
                           AutoPostBack="True"
                           DropDownStyle="DropDown"
                           AutoCompleteMode="SuggestAppend"
                           CaseSensitive="False"
                           CssClass="WindowsStyle"
                           ItemInsertLocation="Append"
                  onselectedindexchanged="ComboBox1_SelectedIndexChanged">
              </asp:ComboBox>


code behind:
CartridgeDB objCartridgeDB = new CartridgeDB();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt = objCartridgeDB.GetAllCartridgesForInventoryAdd("");
                ComboBox1.DataSource = dt;
                ComboBox1.DataTextField = "Name";
                ComboBox1.DataValueField = "id";
                ComboBox1.DataBind();

            }
        }

        protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                txtSelectedProduct.Value = ComboBox1.SelectedValue.ToString()+","+ComboBox1.SelectedIndex.ToString();
                hdnSelectedProductId.Value = ComboBox1.SelectedIndex.ToString();

        }


But i'm getting error.

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) +9879947
   AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in f:\TeamCity\buildAgent\work\80acd78aa4c25314\Server\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:323
   AjaxControlToolkit.ComboBox.OnLoad(EventArgs e) in f:\TeamCity\buildAgent\work\80acd78aa4c25314\Server\AjaxControlToolkit\ComboBox\ComboBox.cs:527
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280


Can any one please help me to solve this issue.

Thanks
Posted
Updated 1-Feb-16 23:59pm
v2

1 solution

The error occurred because the server control is not yet rendered, while the JavaScript code is executed. Go through below solutions:

Solution 1: Put your javascript code to bottom bottom of your page. It means append javascript code after form tag.

Solution 2: It looks like "<%=" present in .aspx file. So try to use <%# instead of <%=
And also add following code in page_load()
C#
protected void Page_Load(object sender, EventArgs e)
{
  Page.Header.DataBind();    
}
 
Share this answer
 
v2
Comments
abdul subhan mohammed 2-Feb-16 5:59am    
i dont have any javascript code, when i'm using ajaxcontrols then i'm getting this error.
[no name] 2-Feb-16 6:31am    
"<%=" is present in your aspx or master page. If that code is present in master page then move it to under <asp:PlaceHolder. if it is aspx page then move that code to under body tag.

http://www.aspsnippets.com/Articles/The-Controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-ie.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900