Topic: Navigation - Displaying a page in a new browser window
Share/Save/Bookmark
C# - Navigation - Displaying a Page in a New Browser Window
 
To start a new instance of the browser, use the client-side "Window" object's "Open" method. You can do this only as part of
a client script because the new window is created on the client. However, there are ways to control the content and appear-
ance of the new browser window from the server.
 
In its simplest form, the "Window:Open" method takes the form shown in below for the following Button HTML control definition:
 
                                                                          v------------------------------------v
<INPUT style="Z" INDEX: 102; LEFT: 55px; WIDTH: 81px; POSITION: absolute; onclick="window.open('Webform2.aspx')" type="submit" value="New Window">
OR TO GO BACK TO THE PAGE BEFORE, YOU CAN USE
onclick="history.back"
To use a variable as the target URL, replace "webform2.aspx" with a data tag:
 
                                                                          v---------------------------------------v
<INPUT style="Z- INDEX: 102; LEFT: 55px; WIDTH: 81px; POSITION: absolute;" onclick="window.open('<%# urlTarget %>')" type="submit" value="New Window">
 
To update the target URL from server code, use a "Public" variable and data binding. The following "Page_Load" event procedure
sets the target URL and updates it with data binding when the page loads:
 
public string urlTarget;
private void Page_Load(object sender, System.EventArgs e)
{
   urlTarget = "Webform2.aspx";
   Page.DataBind();
}
 
Because the "Window.Open" method takes many different arguments to control the various aspects of the new browser window, you
might want to create a class to handle all the various settings. Classes allow you to encapsulate all the possible settings
so that they can be used in an object-oriented manner. The following "Page_Load" event procedure and class definition
demonstrate how to control the size, location, and URL of the new window from server-side code using a class named "BrowserWindow".
 
public BrowswerWindow urlTarget = new BrowserWindow();
 
private void Page_Load(object sender, System.EventArgs e)
{
   urlTarget.URL = "Execute.aspx";
   urlTarget.Top = 100;
   urlTarget.Left = 100;
   urlTarget.Width = 400;
   urlTarget.Height = 400;
   // Update HTML Button
   Page.DataBind();
}
 
// Class to control new browser window create in scripts
// Defualt settings shown here are the same as browswer defaults
public class BrowswerWindow
{
   // String settings; default is blank
   public string URL = "about:blank";
   // Integer settings: 0 invoked default
   public int Height = 0;
   public int Width = 0;
   public int Top = 0;
   public int Left = 0;
   // Boolean-like settings: 0 is "no", 1 is "yes"
   public int ChannelMode = 0;
   public int Directories = 1;
   public int FullScreen = 0;
   public int Location = 1;
   public int Resizable = 1;
   public int ScrollBars = 1;
   public int Status = 1;
   public int TitleBar = 1;
   public int ToolBar = 1;
   public int MenuBar =1;
}
 
The HTML code for a button that uses these settings would look like the following example:
<INPUT style="Z-INDEX: 103; LEFT: 25px; WIDTH 126px; POSITION: absolute; TOP: 60px; HEIGHT: 33px" type="button" value="Show New Window"
onclick="window.open('<%# urlTarget.URL %>, null,
'height=<%# urlTarget.height %>,
width=<%# urlTarget.Width %>,
top=<%<# urlTarget.Top %>,
...
...
...
toolbar=<%# urlTarget.Toolbar %>')">