473,416 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
Nov 19 '05 #1
2 7334
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" <NO*****@NoOneKnows.invalid> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 19 '05 #2
"Axel Dahmen" <NO*****@NoOneKnows.invalid> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
5
by: toylet | last post by:
Attached is some css codes for a website. It has 3 parts: top-bar, side-bar (on the left) and main-body. The top-bar has a mouseover menu called top-menu implemented via ul:hover. When the mouse...
2
by: Shaun | last post by:
Hello! I have a quick question regarding CSS and having it applied to all elements. I am trying to eliminate the gap between a paragraph and a list that usually occurs in html and I've found...
5
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
0
by: Bryan Donaldson | last post by:
I have a table on my web form, declared like this: <asp:table id="tblOverrides" runat="server" enableviewstate=true" cssclass="clsTable"> <asp:tablerow cssclass="clsTblHeader"> <asp:tablecell ...
3
by: Henry Johnson | last post by:
Okay - I'm spinning my wheels on this one... can someone help me figure out how to programmatically populate a table cell as follows (from C# code-behind)? I've tried using a Literal control in the...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
0
by: Dan | last post by:
Hi, I get this error: "Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). " when trying to create a table in the code-behind file like this: dim...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.