Solved

window.open & RegisterClientScriptBlock

Posted on 2006-06-13
6
756 Views
Last Modified: 2008-03-10
Hi,

I have an ASP .NET (1.1) page in C# with a Text Box and a button. The user enters some HTML code into the text box and presses the button for a preview. The server side code saves the entered HTML to an *.htm file and uses RegisterClientScriptBlock to have the client browser open the preview file in a new window.

private void btnPreview_Click(object sender, System.EventArgs e)
{
   string               html         = "";
   string               filename      = "";
   System.IO.StreamWriter   streamWriter   = null;

   html   = this.txtScript.Text; // This is the text box in which the user entered some HTML code

   try
   {
      filename   = this.Server.MapPath("ScriptPreview.htm");

      streamWriter   = System.IO.File.CreateText(filename);
      streamWriter.Write(html);
      streamWriter.Close();

      this.RegisterClientScriptBlock("Preview", "<SCRIPT LANGUAGE=\"javascript\">window.open(\"Preview.htm\");</SCRIPT>");
   }
   catch
   {
   }
}

I know the code works because when viewing the HTML source of the resulting page, the newly inserted script is visible but has no effect and no window is opened.

Any ideas?

Thanks,

Khan
0
Comment
Question by:raheelasadkhan
  • 3
  • 2
6 Comments
 

Author Comment

by:raheelasadkhan
Comment Utility
I tried replacing the following line:

this.RegisterClientScriptBlock("Preview", "<SCRIPT LANGUAGE=\"javascript\">window.open(\"Preview.htm\");</SCRIPT>");
  with
this.RegisterClientScriptBlock("Alert", "<SCRIPT LANGUAGE=\"javascript\">alert(\"sdfsdfsdf\");</SCRIPT>");

And this works fine!!!

What am I doing wrong with the original statement.
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
Hi,
The above code opens the .htm page for me.
Do u want to open the dialog box or new page?

--pradeep
0
 
LVL 14

Expert Comment

by:existenz2
Comment Utility
The path to Preview.htm might be incorrect

An additional note:

For security reasons I would suggest you to use a literal control in a default not-editable page to fill the created html page in. A lot safer (+ more controlable).
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:raheelasadkhan
Comment Utility
pradeepsudharsan:
Hmmm... I tried it on two different machines and did not work on either. I had considered a Dialog but that wouldn't work with netscape right. I need this to be compatible with at least IE and Netscape and preferably FireFox as well.

existenz2:
I verified the path to Preview.htm by viewing the source of the generated page and copy-pasting the url in the browser.
Regarding the literal control, I'm not sure what you mean. Please explain.
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 500 total points
Comment Utility
A literal control is a control in which you can include through you code behind dynamicly any form of html. Basicly it's just a empty placeholder for pure html. By creating pages from the html people have provided you allow them to possibly ad dangerous code which could do harm to your system. Using a literal control allows you to control this much better.
0
 

Author Comment

by:raheelasadkhan
Comment Utility
I tried the literal control and am able to display the preview on the same page. This is nice but I have one question. Since you have already solved my primary problem, it's fair to post the new question as a new post. I'm accepting this answer and opening a new question. If you're interested, please check out (http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21887812.html).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now