using Response.Clear / remove generated html code

E

Eirik Eldorsen

Is it possible to clear the generated html ouput on aspx page, and then
write something else?

Why? :)

I want to only the html code from a panel, and most important no form tags.
Why? I'm setting contenttype to application/rtf to generate a word file from
the code, and in Word 2003 the form tag is not an invisible element.

I've tried putting my code in Page_Unload but i'm not allowed to user
Response.Write here. Are there any other options?

I use this code to get the html code from the panel:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htmlTW = new HtmlTextWriter(sw);
pnlContent.RenderControl(htmlTW);

string panelHTML = sb.ToString();

Response.Clear();
Response.Write(panelHTML);



Eirik
 
G

Guest

If you make the its own ASPX page with nothing in it except the Page
directive at the top then you can send what you like in that page's Render()
method - a Page.Response.TransmitFile() call seems to work quite well. At
least this works for me, although there may be a "better" way of doing this.

- Lee
 
E

Eirik Eldorsen

Thanks.

Yes I had thought of that. But then I would have to put my html code in a
session variable, and then redirect to the new page, and then remove the
session variable. It would work fine, but I though there might be a better
way.
 
J

jasonkester

You might be able to get away with simply lopping off what you don't
want from the .aspx page. Intellisense will complain when it sees
elements out of place, but it will still compile and render as you
would expect. I do this quite a bit to build XML files for AJAX
responses.

In your specific case, if you want to spit out a word document, you
would do the following once you had your panelHTML string ready:

Response.ContentType = "application/rtf ";
Response.AddHeader("content-di­sposition", "attachment; filename=" +
FileName);
Response.Write(panelHTML);
Response.End();


Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
E

Eirik Eldorsen

Thank you! :-D

A perfect solution which i'll probably find a use for in future problems.

Eirik

"jasonkester" <[email protected]> skrev i melding
You might be able to get away with simply lopping off what you don't
want from the .aspx page. Intellisense will complain when it sees
elements out of place, but it will still compile and render as you
would expect. I do this quite a bit to build XML files for AJAX
responses.

In your specific case, if you want to spit out a word document, you
would do the following once you had your panelHTML string ready:

Response.ContentType = "application/rtf ";
Response.AddHeader("content-di­sposition", "attachment; filename=" +
FileName);
Response.Write(panelHTML);
Response.End();


Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top