1

I am using asp.net 3.5 and want to render the contents of a static html file into an asp.net page that contains a master page in one of the contentplaceholders. I have considered using an iframe but would like to avoid that if possible. Alternatively, if it's simpler I would like to just read in some text in a file that's html like:


<table> <tr> <td> Hey dude <td> </tr> </table> <div> <span>I am special! </span> </div> 

and render that into that contentplaceholder in the page. I have thought about a literal control to do this but am not sure if that will work either. The problem I am trying to solve is that I want to be able to give a user access to an html page or text file and have them update it with content. Then at runtime render the html markup from this file. This is the only page in the application I need this functionality so something like a CMS system or html editor control is overkill. I just need to read a in a file and render the html markup in an asp.net page. Also I would like the markup that's rendered to use the css we already have but I think that's a gimme either way.

3 Answers 3

3

I would read the text from an external text file as you described above into memory similar to what is described in MikeB's solution then turn that into a LiteralControl that you can add to your page.

protected void Page_Load(object sender, EventArgs e) { LiteralControl literal = new LiteralControl( GetExternalHTML() ); Page.Controls.Add(literal); } private string GetExternalHTML() { StreamReader sr; string html; sr = File.OpenText("<path_to_file.txt>"); html = sr.ReadToEnd(); sr.Close(); return html; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Sounds like the best way for what I am trying to do. Thanks!
2

You could use Server Side includes:

http://msdn.microsoft.com/en-us/library/3207d0e3(VS.71).aspx 

Or Response.WriteFile:

http://support.microsoft.com/kb/306575 

Since it's included into your page it would certainly take on whatever CSS styles you are using.

1 Comment

Thanks, never heard of Server Side includes.
0

If you're going with the Literal, make sure you set:

literal.Mode = LiteralMode.PassThrough; 

Otherwise, you might create a special WebControl (or inherited from Literal) to handle this task. You could then override the Render(HtmlTextWriter writer) or similar with your own method, retrieving the file you need to render and then write it to the stream.

Another option could be to simply call the Page.Response.WriteFile(filename). I wouldn't recommend that however, but it's an option. :) The problem with Response is that it won't necessarily happen when you might expect it to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.