Code blocks usage RRS feed

  • Question

  • User-1765853593 posted

    I'm using template engine similar to master pages but for ASP.NET 1.1.

    And I cannot use code blocks there because of when templating is performed an error occurs:
    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

    But I still find code blocks very useful and want to use it, for example to emit HTML elemnt IDs to scripts.

    Is there any way to deal with that?


    Thursday, April 12, 2007 5:00 AM

All replies

  • User945078486 posted

    Hello Dmitriy,

    This error happens when asp.net encounters code blocks like <%= %>, or <%# %>, in a User Control and the code-behind the User Control is trying to modify the controls collection of the page (usually a LoadControl statement).

    Refer to this thread



    hope it helps,


    Thursday, April 12, 2007 11:43 PM
  • User-1765853593 posted

    Hello Jessica,

    Thank you for very informative links.

    But what is the best way in your opinion to emit control's ClientID into scripts with Master Pages?


    Friday, April 13, 2007 1:53 PM
  • User945078486 posted

    Hi Dmitriy,

    I think you can use

            ContentPlaceHolder cp = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
            WebUserControl wb = cp.FindControl("WebUserControl1") as WebUserControl;
            TextBox tb = wb.FindControl("TextBox1") as TextBox;
            tb.Text = "abc";

     Hope it helps,


    Wednesday, April 18, 2007 3:31 AM
  • User-1765853593 posted

    Hi Jessica,

    Yes we always can use code like this.
    But the thing is most usefull for me with code bloack is emiting server control's ID just into page like this.



    <script type="text/javascript">
    function someJSFunction()
      var el = getElementById('<%= SomeServerControl.ClientID %>');
      // do something with element



    Wednesday, April 18, 2007 7:51 AM