Question

Hy,

In my application I store a string as content of a html file.

How can I preview this content (assuming that it's modified from original content) in browser but not having to save it to disk locally.

And the preview to be in another tab or window.

Does anyone have any idea?

Thanks in advance.

Jeff

Was it helpful?

Solution

Save the content to a Session variable and make a dummy page and load to content from the Session variable in the Page Load event.

Where your temp content are created

Session["TempPage"] = Content;

And then have a ShowTempPage.aspx that is empty with code behind Page Load event:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(Session["TempPage"]);
}

Something like that could work..

Update: To open the temp page in a new window create a link like this on the main page:

<a href="http://localhost/ShowTempPage.aspx" target="_blank">View temp Page</a>

OTHER TIPS

Create an HTTP Handler, and in the implementation of IHttpHandler serve the HTML content from memory. Use the correct target attribute on the link to force new tab/window on the client.

(That's based on the asp.net tag on the question: you're writing a web site. If this is a local application (WinForms or WPF) then you can embed a browser control and set its NavigateToString method to the HTML text.)

I would use an iframe and a piece of javascript. Use javascript and document.write to fill the iframe window.

Is the question mostly just how to open a new window? This depends on what prompts the action. If it's a hyperlink, just add target="_blank". If not or you want more control over the window then use javascript.

Showing content without saving it to disk is the very nature of server-side code, you can't have a web site without doing this.

Response.Write(myString)

Here's a Javascript/JQuery solution:

<!DOCTYPE html>
<html>
<head>
<title>Open Custom HTML Page Without Saving</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
  $('.preview-site').on('click', function(){
    window.open('javascript:document.write("'+ $('.workflow').val() +'")', 'Opened Page', 'width=660, height=440');
    return false;
  });
});
</script>
</head>
<body>
<a class="preview-site" title="Preview your workflow" href="javascript:void(0)">Preview your workflow</a><br/>

  <textarea class="workflow"><!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html></textarea>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top