Generating HTML code in the code-behind

L

lisa

Can I do this?

I'm playing with AJAX, and it occurred to me that it'd be a lot easier
to make changes to a control on the server, grab the HTML that control
would now render, and return that to the client, where Javascript could
just use outerHTML to replace the whole control.

The obvious catch is generating that HTML.

I tried making an HTMLTextWriter and doing control.Render(writer), but
that gave me an error.

I don't want to override the Page's Render method, because I want to
make this fairly self-contained. Just stick it in a helper function in
a helper class, basically.

And after banging my head against it for a few days, I thought I'd ask
if anyone knows (a) how to do it, or (b) whether it can be done.

Thanks muchly,
Lisa
 
A

Anton

intrader said:
I have not tried it, but you should be able to instantiate a control and
ask it to render to a stream.

What I have done requires overridint the Render method. There you should
create a StreamBuffer which you use to create an HTMLTextWriter, then you
tell the base to render itself, and voila, you have the HTML in the
StreamBuffer.

Lastly OuterHTML is not available on Firefox. It's a Web browser. :p
Just so you know. ;)
 
J

jjrdk

I'm not quite sure what you mean by this. Of course you can render
javascript in a control and have it refer to the HTML that the control
renders. There are certain pitfalls that you should observe regarding the
way the control's ID may be generated on complex pages.

The easiest way to insert javascript is to use the
"regiserclientscriptblock" or "registerstartupscript" in the control.

Anyway it would be easier to understand if you gave some more details about
what you were trying to do.

jjrdk
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top