Solved

Opening Windows Programmatically from Code Behind

Posted on 2008-06-09
8
1,419 Views
Last Modified: 2010-04-21
I have an ASP.NET 2.0 application that is generating a number of HTML files in a code behind file. For each of these HTML files I would like to open a new browser window (the HTML files are reports). I have tried using javascript and have been unable to get the window to open up.



0
Comment
Question by:wframsay
8 Comments
 
LVL 18

Expert Comment

by:ChetOS82
ID: 21746309
What are you doing in Javascript?  After the HTML files are created, generate a window.open(url); line in the javascript, when the page loads it should execute all of them and open the windows.
0
 
LVL 1

Author Comment

by:wframsay
ID: 21746433
I do not know how many files are going to be generated (it's based on user entry). Ideally I would open each of the HTML files in a window as each item is processed (some items do not generate reports, others do, so there could be 10 reports to display or just 1 or even 0).

How do I "generate a window.open(url) line in javascript" for multiple files?
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 21746634
>>generating a number of HTML files in a code behind file. For each of these HTML files I would like to open a new browser window

If you're generating complete HTML files dynamically, and you want to open them up in a new window, you don't even have to store the html "pages"...

below is an example of generating completely dynamic html pages and then opening them
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void Page_Load(object sender, EventArgs e)
    {
        string StrPage = "";
        for (int i = 0; i <= 4; i++)
        {
            StrPage = "<html><head><title>Page " + i.ToString() + "</title></head><body>This is a completely dynamic page.<br />Page: " + i.ToString() + "</body></html>";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript" + i.ToString(), "<script language=\"javascript\">openDynamicPage('" + StrPage + "','" + i.ToString() + "');<" + "/script>");
        } 
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Main Page</title>
 
    <script language="javascript" type="text/javascript">
	function openDynamicPage(htmlText,winID)
	{
		var printWin = window.open("","DynamicPage"+winID);
		printWin.document.open();
		printWin.document.write(htmlText);
	}
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:wframsay
ID: 21747353
The report files that are generated have to be kept on the server, rather than just displayed and then discarded, so I do not think this solution will work for me. Thank you for the effort, though.
0
 
LVL 6

Expert Comment

by:cottsak
ID: 21748271
wframsay,

i think samtran0331's idea might work 4 u actually!
how do you get the generated html reports? a url link or the html stream?
0
 
LVL 1

Author Comment

by:wframsay
ID: 21750103
I agree that the solution would work if I were able to display them as they were generated, however we are required to store the files for future reference, so I need to create actual html files. For the purposes of the demo I am putting together I am using something similar to the suggestion provided.
0
 
LVL 1

Author Closing Comment

by:wframsay
ID: 31465538
For the purposes of the demo I am putting together I am using something similar to the suggestion provided, so I'm accepting the solution provided.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 21750264
>>however we are required to store the files for future reference, so I need to create actual html files.

That being the case, you can still use the loop like in my example, but then the js function would be more like what ChetOS82 eluded to.

the loop would be driven by whatever logic you have to determine which reports are shown and would be more like:

    protected void Page_Load(object sender, EventArgs e)
    {
        string StrPageURL = "";
        for (int i = 0; i <= 4; i++)
        {
            StrPageURL = "somepagename.html"; //be sure to set the path correctly
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript" + i.ToString(), "<script language=\"javascript\">openDynamicPage('" + StrPageURL + "','" + i.ToString() + "');<" + "/script>");
        }
    }


And the js would be:
    <script language="javascript" type="text/javascript">
      function openDynamicPage(htmlURL,winID)
      {
            window.open(htmlURL,"DynamicPage"+winID);
      }
    </script>



ultimately, the trick to opening multiple windows is the "winID" in the js function, as long as each iteration has a unique winID, the browser will open a new window...
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete HTML table rows 12 29
Question! 4 36
Problem to refer to value 8 50
Obtain cell value using column ID in DataGrid using VB.NET 2 19
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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