Opening a new window from code behind on Button Click in ASP.NET

If you are looking out for server side code to open a new window on Button Click, then here's how to do so.

Add the following script to the <head> section of your page

<script language="javascript" type="text/javascript">

        function openNewWin(url) {

            var x =, 'mynewwin', 'width=600,height=600,toolbar=1');




Then add a Button Control in the following manner

    <asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"           

        onclick="btnOpenPop_Click" />

Finally add some code in the code behind file


    protected void btnOpenPop_Click(object sender, EventArgs e)


        string url = "";

        ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");



    Protected Sub btnOpenPop_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim url As String = ""

        ClientScript.RegisterStartupScript(Me.GetType(), "OpenWin", "<script>openNewWin('" & url & "')</script>")

    End Sub

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


JeffMc said...

Have you tested this on a real site besides localhost? It's almost certainly going to be blocked by your popup blocker. It won't be blocked on localhost because web pages hosted there are considered to be in the "Local Intranet Zone" and are therefore "trusted" (in IE at least). OR, perhaps you've already allowed popups from this site you're testing?

In any case, this can't be done reliably using your technique. New windows can only be opened by direct user action - not during the page load process (which would be considered indirect). And even then, it has to be done in a true onclick() or javascript href - not even a onmousedown() will work.


JeffMc said...

So yeah, just to follow up... I knew from experience this technique doesn't work, but I suppose it wasn't helpful to just poopoo your blog post without helping you out...

For the record, I just tested your exact code on IE 6, 7, 8, Firefox, Safari, and Opera and every single one of them blocked the So, it's not a good technique - sorry.

A better way to do it is to perform a Synchronous AJAX call (so, a "JAX" call I guess :) ) Because it's synchronous, you'll get the result back AND open the window all in the onclick() handler, thus satisfying the "direct user action" rule and preventing it from being blocked.

So, the process would be (in psuedo-code, of course):

function button_click()
// Do synchronous XmlHttpRequest here
var result = doSyncCallToServer();"YourPage.aspx?Param=" + result);


Suprotim Agarwal said...

Thanks Jeff for sharing your solution.Indeed that's true that the pop-up blocker (if enabled) will block the window. The solution I have given is a quick and dirty one considering that an entry has been made for the site in the pop-up blocker settings( Tools->Pop-up Blocker->Pop-up Blocker Settings)

One way of detecting the pop-up blocker is to use this bit of code:

<body onload="IsPopUp();">

function IsPopUp()
var pop = true;
pop ='some.htm','','width=100,height=100,left=0,top=0,scrollbars=no');
alert('Pop Up Enabled');

SVK said...

i tried the above code metheod for openig a new window
but get blocked by popup blocker
is there a way to avoid the block and open the window
i am new to ajax if its possible in ajax , how do i do it
at present i am using on button click

Response.Write("script>" & "('test.aspx?id=" & code.Value & " ','_new','width=800,height=800');/script")

i have remove <> after and before the script word

Jim said...

Thanks, you solved my problem.

.net developer said...

You can use ajax to improve user experience,
Open new window from code behind in ASP.NET

Anonymous said...

great code in the nick of time it has helped me alot
a newby

Anonymous said...

Thanks for the help!!

SvendK said...

I clicked ".net developer"'s link above, but it was 401. Searched his site, this is the new hideout for the article:

I do it this way instead, but when dived into the .net world, it might not be best practice to do it totally clientside. I just dont like ScriptManager and its thousands of autogenerated code, when I can do it in a simple js onclick method...

<input type="button" id="btnName" value="Click me" onclick=";" />