Click here to Skip to main content
15,886,796 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
2.13/5 (17 votes)
5 Jul 2007CPOL3 min read 142.1K   1.5K   20   11
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:

NameValueDescription
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 numberSpecifies the top position, in pixels.
widthnumberspecifies 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)


Written By
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? Pin
mahmoud Noraei9-Sep-12 20:33
mahmoud Noraei9-Sep-12 20:33 
Questionopen page by typing URL in address bar Pin
kumarjayesh5-Jun-12 1:34
kumarjayesh5-Jun-12 1:34 
GeneralMaster page problem Pin
shawpnendu24-Feb-08 23:03
shawpnendu24-Feb-08 23:03 
GeneralQuickie but good article Pin
ChristopherHenderson7-Sep-07 11:59
ChristopherHenderson7-Sep-07 11:59 
GeneralRe: Quickie but good article Pin
Mostafa M. Zain10-Sep-07 4:04
Mostafa M. Zain10-Sep-07 4:04 
GeneralYou could do better Pin
Haissam6-Jul-07 10:58
Haissam6-Jul-07 10:58 
GeneralVery Poor Pin
Cetin Yilmaz6-Jul-07 3:28
Cetin Yilmaz6-Jul-07 3:28 
GeneralRe: Very Poor Pin
Mostafa M. Zain6-Jul-07 3:39
Mostafa M. Zain6-Jul-07 3:39 
GeneralRe: Very Poor Pin
Cetin Yilmaz6-Jul-07 3:59
Cetin Yilmaz6-Jul-07 3:59 
GeneralRe: Very Poor [modified] Pin
Mostafa M. Zain6-Jul-07 4:10
Mostafa M. Zain6-Jul-07 4:10 
you are completly and professionaly right but all i am aiming at on the point you are discussing is to let people know the Property ClientScript and use it.However,the way you mentioned is more professional.
thank you


-- modified at 10:18 Friday 6th July, 2007
GeneralRe: Very Poor Pin
Not Active6-Jul-07 7:32
mentorNot Active6-Jul-07 7:32 

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

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