The client script or the JavaScript will be written and registered at the server side simultaneously while fetching the files from a folder.
Earlier we have written an article on how to register and call JavaScript from code behind, and we recommend our users to go through this article too.
Using Asp.Net System.IO namespace, we will fetch the files from a folder in the server. Once fetched, we will loop through the list of files and register each file with JavaScript using Asp.Net ClientScript.RegisterStartupScript method, so that each file will open in a new window.
The ClientScript.RegisterStartupScript method takes four parameters to register the startup script. The first parameter is the type of the startup script. The second parameter is a string value for the key of the script. The third parameter is the script (or the function you wrote the script). The forth parameter is addScriptTags, Boolean (true or false) indicating whether to add script tags.
Page.ClientScript.RegisterStartupScript(this.GetType, “Popup_File”, “window.open();”, true);
Ref: ClientScriptManager.RegisterStartupScript Method
Overview
The main highlight of the above script is the type this.GetType (Me.GetType in Vb.Net) and the key value Popup_File. We must always add a unique type and key combination for all the registered scripts on a single page. Since each script will be uniquely identified using this combination. Therefore, we will add a File Counter with the KEY to keep it unique.
C#
“Popup_File” + iFileCnt;
Vb.Net
“Popup_File” & iFileCnt
Note: You can also use Asp.Net “GUID” instead of a counter, to generate unique ID’s.
C#
“Popup_File” + Guid.NewGuid();
Vb.Net
“Popup_File” & Guid.NewGuid()
window.open() function
The open() method will open a new browser window.
Syntax:
window.open (url, name, specifications, replace (boolean))
• url – The url or the location of the web page which is to be opened.
• name – The target attribute of the page. E.g. _blank.
• specifications – Define the necessary features of the newly created or opened window. Like, should it have a toolbar, the address bar or should the window be sizeable etc.
• replace – True if the new location will replace the current page in the history list.
Now let us see how we can open multiple files in multiple browser windows using all the above mentioned features.
Please Note: Your browser may not allow windows to popup by default, so you need to explicitly set the browser accordingly to allow the windows to popup.
We just need a button to do a post back to the server to fetch the files and then popup on new windows.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Open or Popup Multiple Browser Windows</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" runat="server"
onserverclick="ShowWindow" value="Show Files" />
</div>
</form>
</body>
</html>
using System; using System.IO; public partial class _Default : System.Web.UI.Page { protected void ShowWindow(object sender, EventArgs e) { // THE FOLDER PATH WHERE WE HAVE THE FILES. DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("Files")); FileInfo[] objFI = objDir.GetFiles("*.pdf"); // LOOK FOR PDF's ONLY. int iFileCnt = 0; // COUNTER TO CREATE UNIQUE KEYS FOR EACH SCRIPT. if (objFI.Length > 0){ foreach (FileInfo file in objFI) { // REGISTER AND OPEN A NEW WINDOW. Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup_File" + iFileCnt, "window.open('https://localhost:555/csharp/files/" + objFI[iFileCnt].Name + "','_blank');", true); iFileCnt += 1; } } } }
Option Explicit On Imports System.IO Partial Class _Default Inherits System.Web.UI.Page Protected Sub ShowWindow(ByVal sender As Object, ByVal e As EventArgs) Dim objDir As New DirectoryInfo(Server.MapPath("Files")) ' THE FOLDER PATH WHERE WE HAVE THE FILES. Dim objFI As FileInfo() = objDir.GetFiles("*.pdf") ' LOOK FOR PDF's ONLY. Dim iFileCnt As Integer = 0 ' COUNTER TO CREATE UNIQUE KEYS FOR EACH SCRIPT. If objFI.Length > 0 Then For Each file As FileInfo In objFI ' REGISTER AND OPEN A NEW WINDOW. Page.ClientScript.RegisterStartupScript(Me.GetType, "Popup_File" & iFileCnt, _ "window.open('https://localhost:555/vb/files/" & objFI(iFileCnt).Name & "','_blank');", True) iFileCnt = iFileCnt + 1 Next End If End Sub End Class