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

A

Axel Dahmen

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
 
A

Amit

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
 
J

John Saunders

Axel Dahmen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top