Generating HTML Client Objects Just Once

G

Guest

I want to create an ASP.NET class which generates some client-side HTML
objects on a page with the following rules:

1. Only generate these objects from the class if they haven't already been
inserted by another instantiation of this class. So if the obejcts have
already been inserted, determine that this is the case and don't do it again.

2. Insert these HTML objects at the very end of the <BODY> of the page,
preferably without the <BODY> having been set as a runat="server".

So there are a few things I don't understand:

a) How do I check a page I'm processing on the server to see if HTML
elements have been dumped into the page with certain IDs, etc?

b) How do I indert these elements at the very end of the <BODY> element?

Alex
 
C

Cowboy \(Gregory A. Beamer\)

Add a Panel control just before </body> tag and add controls to the Panel
controls collection.

Panel1.Controls.Add(ControlName)

You can then search to see if the controls have been added to the panel when
you are rendering other controls that would fill the Panel with the same
control.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
G

Guest

I dig that. Except one thing: I don't want to have to know ANYTHING about the
page into which I'm trying to dump this stuff. Meaning, I won't be able to be
sure that the Panel you're suggesting actually exists on the page. Pretty
much the only thing I want to insist upon is that the basic <body></body> be
present. Any way I can do this?

Thanks!

Alex
 
W

Walter Wang [MSFT]

Hi Alex,

Thank you for your post.

Based on my understanding, your question is how to insert certain content
into the generated html source. If I've misunderstood anything, please feel
free to post here.

I think you can override the Render method of Page class to directly change
the generated html source:

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter tw = new HtmlTextWriter(new StringWriter(sb));
base.Render(tw);
sb.Replace("</body>", "<a href=\"#\">Hello</a></body>");
writer.Write(sb.ToString());
}

Above sample code is using a simple replacement and not checking if the
replacement is already done. But you can use Regular Expression to do that
easily.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hans Kesting

I want to create an ASP.NET class which generates some client-side HTML
objects on a page with the following rules:

1. Only generate these objects from the class if they haven't already been
inserted by another instantiation of this class. So if the obejcts have
already been inserted, determine that this is the case and don't do it again.

2. Insert these HTML objects at the very end of the <BODY> of the page,
preferably without the <BODY> having been set as a runat="server".

So there are a few things I don't understand:

a) How do I check a page I'm processing on the server to see if HTML
elements have been dumped into the page with certain IDs, etc?

b) How do I indert these elements at the very end of the <BODY> element?

Alex

Maybe you can use the RegisterStartupScript method for this. It's
designed to insert script blocks (at the end of the html), but as you
are required to add the surrounding <script> tags, maybe plain html
will work as well.
This method requires two parameters. The second is the block of code to
insert, and the first is a "key". You can use IsStartupScriptRegistered
to find out is there is already a block with that key (if there is, you
don't want to insert it again).

Hans Kesting
 

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

Top