Click here to Skip to main content
Click here to Skip to main content
Go to top

How To Open a Web Page In a New Browser ASP.NET?

, 5 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Opening web pages in new web browser windows

Introduction

Many Beginer Web Developers wonder how they can open a new web page in a new web browser and setting some options for it.

In this article I will discuss two methods of opening a new web page in a new web browser. The first method is by using the HyperLink control and the other is by using JavaScript.

Using the HyperLink Control

It is simply an easier way; you only have to set the value of the property "Target" to "_blank" as shown:

Screenshot - Target.jpg

Using JavaScript

This section will be divided it into two sub-sections; they are somehow the same but with minor differences. The sub-sections will be one for the client side controls and the other for the server side controls.

For both we will use the same JavaScript method "window.open( )". We will first explain the parameters of this method then know how to use it. The syntax is given below:

window.open( [sURL] [, sName] [, sFeatures] [, bReplace])
  • URL: Optional. The URL of the page to open in the new window. This argument could be blank.
  • Name: Optional. A name to be given to the new window. The name can be used to refer this window again.
  • Features: Optional. contains a list of items separated by commas. Each item consists of an option and a value, separated by an equals sign (like status bar, address bar etc).
  • Replace: Optional. Boolean that specifies whether the sURL creates a new entry or replaces the current entry in the window's history list. This parameter only takes effect if the sURL is loaded into the same window.

After being familiar to the parameters of the method I will list some of the features that you can use:

Name Value Description
fullscreen {yes|no|1|0} Specifies whether to display the browser in full-screen mode.
height number Specifies the height of the window in pixels
left number Specifies the left position, in pixels.
top number Specifies the top position, in pixels.
width number specifies the width of the window in pixels.
location {yes|no|1|0} Specifies whether to display the navigation bar.
menubar {yes|no|1|0} Specifies whether to display the menu bar.
resizable {yes|no|1|0} Allow/Disallow the user to resize the window.
scrollbars {yes|no|1|0} Enable the scrollbars if the document is bigger than the window.
status {yes|no|1|0} Specifies whether to add a Status Bar at the bottom of the window.
titlebar {yes|no|1|0} Specifies whether to display a Title Bar for the window.
toolbar {yes|no|1|0} Specifies whether to display the browser command bar, making buttons such as Favorites Center, Add to Favorites, and Tools available.

First I will talk about using the window.open() method in the client side. All what you need to do is to write this line of code in the function of the client side control:

function Button2_onclick() 
{
window.open('Default2.aspx','PoP_Up','width=500,height=500,menubar=yes,toolbar=yes,resizable=yes,fullscreen=1');
}

This line of code will open the "Default2.aspx" web page in a new browser with the features set on the function parameters.

Second I will talk about using the same method but in the code behind which means in the server side. Lets check this block of code and explain what will happen :

 protected void Button1_Click(object sender, EventArgs e)
    {
      //Part 1 the same code as before
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(>script language='javascript'>");
        sb.Append("window.open('Default2.aspx', 'CustomPopUp',");
        sb.Append("'top=0, left=0, width=500, height=500, menubar=yes,toolbar=yes,status=1,resizable=yes'));

        sb.Append("/script>");
      
    //Part 2 Registering The ClientScriptBlock
        Type t = this.GetType();

if (!ClientScript.IsClientScriptBlockRegistered(t, PopupScript))
ClientScript.RegisterClientScriptBlock(t, PopupScript, sb.ToString());

In part 1 it is the same code as in the client side subsection. Without part 2 the page "Default2.aspx" will not be opened because you have not register the client script block yet. In the code you check for the script block by its name(key),if not exist then you add it else nothing happens.

I encountered something that I found it helpful.what shall you do if you want to make the new browser open in the maximized mode? You can use the properties "screen.availwidth" and "screen.availheight" to set the widht and height of the features of the window.open() method respictivley as follows:

sb.Append('top=0, left=0, width='+screen.availwidth+', height='+screen.availheight+', menubar=yes,toolbar=yes,status=1,resizable=yes'));

I hope this article has been helpful to those of you who queried how to open new web pages in new web browser windows, and I urge those of you who have other comments or tidbits to speak out, and for those with further queries please do not hesitate to ask. Leave a post and we'll get to you as soon as humanly possible!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Moustafa Zain
Web Developer
Egypt Egypt
.NET Developer
Works now as a SoftwareDeveloper and Trainer
 
Academics:
BSc Computer and Information Sciences June 2006.
 
Certifications:
MCAD C#.Net

Comments and Discussions

 
QuestionHow To Open a Web Page In a New Browser ASP.NET? Pinmembermahmoud Noraei9-Sep-12 20:33 
Questionopen page by typing URL in address bar Pinmemberkumarjayesh5-Jun-12 1:34 
GeneralMaster page problem Pinmembershawpnendu24-Feb-08 23:03 
GeneralQuickie but good article PinmemberChristopherHenderson7-Sep-07 11:59 
GeneralRe: Quickie but good article PinmemberMoustafa-Mohammed10-Sep-07 4:04 
GeneralYou could do better PinmemberHaissam6-Jul-07 10:58 
GeneralVery Poor Pinmembercetinyilmaz6-Jul-07 3:28 
GeneralRe: Very Poor PinmemberMoustafa-Mohammed6-Jul-07 3:39 
GeneralRe: Very Poor Pinmembercetinyilmaz6-Jul-07 3:59 
GeneralRe: Very Poor [modified] PinmemberMoustafa-Mohammed6-Jul-07 4:10 
GeneralRe: Very Poor PinmvpMark Nischalke6-Jul-07 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Jul 2007
Article Copyright 2007 by Moustafa Zain
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid