Dynamically adding controls

M

Mike Lowery

I have an ASP.Net page that simply generates a dynamic page using
Response.Write() statements to generate the HTML. This works great except that
one of the things I want to generate is a LinkButton. I know this can be done
by creating a Panel and adding it using Panel.Controls.Add(LinkButton) but the
problem is there are no controls on the page--I want to dynamically add
everything using simple Response.Write statements. But it doesn't look like I
can add a LinkButton this way.

This is an example of what I'm trying to do:

Response.Write("<br>")
Response.Write("This is some text. Next ")
Response.Write("<asp:LinkButton text=""Click"" onclick=""LinkButton_Click()""
/>")

The above doesn't actually create a LinkButton control, which is not entirely
surprising. Controls.Add(LinkButton) doesn't work either because it has to be
within the <Form> area which is why a Panel control is used.

So is there a way to generate a dynamic page like this without having to place
controls on the blank page beforehand?
 
G

Guest

Mike,
That's correct, you can not, because what you are doing in this case is
taking over the normal ASP.NET Page processing model and instead, writing
HTML directly to the Response Stream, which is what the Page class would
normally do.

So, you have a decision to make. ASP.NET controls are created and rendered,
populated, and their events hooked up as a normal part of the Page lifecycle.
If instead you decide you want to write everything directly to the Response
object, you cannot have it "both ways".

Hope that helps.

Peter
 
M

Mike Lowery

Thanks Peter, that's what I was afraid of. Looks like I'll have to slap a table
on there to do what I want, or go a completely different route and use a
TreeView control.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top