ASP.NET can you add controls at run time?

  • Thread starter Thread starter Jax
  • Start date Start date
J

Jax

Lets say i have a list of objects of a variable size.
What I would like to do is create controls and add them to
the web page at run time that represent all the items in
the list.
Problem is everytime i do this:

this.Controls.Add(SomeControl);

The html ends up after the </HTML> tag. Therefore it
doesn't work properly. What do i have to do to make sure
the extra html ends up within the tags?

Many thanks for anyone who clears my cloud of ignorence.

jax
 
Jax said:
Lets say i have a list of objects of a variable size.
What I would like to do is create controls and add them to
the web page at run time that represent all the items in
the list.
Problem is everytime i do this:

this.Controls.Add(SomeControl);

The html ends up after the </HTML> tag. Therefore it
doesn't work properly. What do i have to do to make sure
the extra html ends up within the tags?

Read the documentation.

Use page trace output to see how things are built.

Your problem:

* The complete HTML content of a page is a control within your page*s
controls collection.

So, by adding your control THERE, you add it - beyond the page content.

What you need to od is - add it to a part within the controls hierarchy.

Use a trace (trace.axd, enable it first) to see how the control hierarchy
for your page looks in detail. It is way more complex (as in: thre are way
more controls) than you psosibly think at the moment.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Thankyou for that, it certainly will point me in the right
direction. I just got a new book which i'm sure will also
provide more clarity for my simple mind.
Many thanks.

jax
 
Jax said:
Lets say i have a list of objects of a variable size.
What I would like to do is create controls and add them to
the web page at run time that represent all the items in
the list.
Problem is everytime i do this:

this.Controls.Add(SomeControl);

The html ends up after the </HTML> tag. Therefore it
doesn't work properly. What do i have to do to make sure
the extra html ends up within the tags?

Many thanks for anyone who clears my cloud of ignorence.

Add a PlaceHolder control at the position where you want your list to
appear, then add the dynamically created controls to the PlaceHolder's
control collection.

Cheers,
 
Back
Top