Code blocks usage

Last post 04-18-2007 7:51 AM by nagir. 4 replies.

Sort Posts:

  • Code blocks usage

    04-12-2007, 5:00 AM
    • Member
      160 point Member
    • nagir
    • Member since 01-09-2006, 7:01 PM
    • Australia
    • Posts 178
    Hello,

    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?

    Ragards,
    Dmitriy.

  • Re: Code blocks usage

    04-12-2007, 11:43 PM

    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

    http://blogs.snapsis.com/PermaLink,guid,381f9036-432e-468c-bc2e-b52ab86f12ea.aspx

     http://west-wind.com/WebLog/posts/5758.aspx

    hope it helps,

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Code blocks usage

    04-13-2007, 1:53 PM
    • Member
      160 point Member
    • nagir
    • Member since 01-09-2006, 7:01 PM
    • Australia
    • Posts 178

    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?

    Cheers,
    Dmitriy.

  • Re: Code blocks usage

    04-18-2007, 3:31 AM

    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,

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Code blocks usage

    04-18-2007, 7:51 AM
    • Member
      160 point Member
    • nagir
    • Member since 01-09-2006, 7:01 PM
    • Australia
    • Posts 178

    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.

    Page.aspx

    ...

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

    ...

    Cheers,
    Dmitriy.

Page 1 of 1 (5 items)