Can't dynamically add controls because of <%...%> code blocks

Discussion in 'ASP .Net' started by Axel Dahmen, Jan 4, 2005.

  1. Axel Dahmen

    Axel Dahmen Guest

    HI,
    I want to dynamically add controls to a web page from within a common base
    class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The
    Controls collection cannot be modified because the control contains code
    blocks (i.e. <% ... %>)."

    All of my pages contain these kind of blocks, for several reasons. For one,
    I don't know any other way to dynamically add header information like e.g.
    style sheet links to a web page. So, is there some way to dynamically add
    controls to a web page while keeping these code blocks?

    To be precise: I've derived a base class from Page (MyPage). All my pages
    are derived from MyPage. In the Page_Load event of MyPage I want to add
    navigation controls at the top of all pages where appropriate.

    Is there a way to achieve this without writing a web control and add it to
    each and every page?

    TIA,
    Axel Dahmen
    Axel Dahmen, Jan 4, 2005
    #1
    1. Advertising

  2. Axel Dahmen

    Amit Guest

    The way I've seen everyone do this is to use a User control. For example,
    Header.ascx has the banner, links etc and Footer.ascx has the footer. These
    controls are placed on all the aspx pages.

    BTW its possible to dynamically add information in the <HEAD> tag. Add
    runat="server" to this tag, declare it as a HtmlGenericControl in the code
    behind and then you can use it programmatically.
    amit



    "Axel Dahmen" <> wrote in message
    news:%...
    > HI,
    > I want to dynamically add controls to a web page from within a common base
    > class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The
    > Controls collection cannot be modified because the control contains code
    > blocks (i.e. <% ... %>)."
    >
    > All of my pages contain these kind of blocks, for several reasons. For

    one,
    > I don't know any other way to dynamically add header information like e.g.
    > style sheet links to a web page. So, is there some way to dynamically add
    > controls to a web page while keeping these code blocks?
    >
    > To be precise: I've derived a base class from Page (MyPage). All my pages
    > are derived from MyPage. In the Page_Load event of MyPage I want to add
    > navigation controls at the top of all pages where appropriate.
    >
    > Is there a way to achieve this without writing a web control and add it to
    > each and every page?
    >
    > TIA,
    > Axel Dahmen
    >
    >
    Amit, Jan 4, 2005
    #2
    1. Advertising

  3. "Axel Dahmen" <> wrote in message
    news:%...
    > HI,
    > I want to dynamically add controls to a web page from within a common base
    > class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The
    > Controls collection cannot be modified because the control contains code
    > blocks (i.e. <% ... %>)."
    >
    > All of my pages contain these kind of blocks, for several reasons. For
    > one,
    > I don't know any other way to dynamically add header information like e.g.
    > style sheet links to a web page. So, is there some way to dynamically add
    > controls to a web page while keeping these code blocks?


    No, but there are better ways to dynamically add content.

    Any HTML tag that you declare with runat="server" can be accessed from your
    codebehind. Among other things, you can use:

    <head id="header" runat="server">
    </head>

    You can then refer to the header in your codebehind and you can add controls
    to it:

    Dim ctl As HtmlGenericControl

    header.Controls.Add(New LiteralControl("<title>Page title</title>"))
    ctl = New HtmlGenericControl("meta")
    ctl.Attributes("http-equiv") = "Content-Type"
    ctl.Attributes("content") = "text/html; charset=windows-1252"
    header.Controls.Add(ctl)
    ctl = New HtmlGenericControl("link")
    ctl.Attributes("href") = "styles.css"
    header.Controls.Add(ctl)

    > To be precise: I've derived a base class from Page (MyPage). All my pages
    > are derived from MyPage. In the Page_Load event of MyPage I want to add
    > navigation controls at the top of all pages where appropriate.
    >
    > Is there a way to achieve this without writing a web control and add it to
    > each and every page?


    Yes. Your base page can add the controls to the Controls collection of the
    HtmlForm control which is (probably) located at Page.Controls(0).

    John Saunders
    John Saunders, Jan 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page