March 16, 2009

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 = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');


            x.focus();


        }


</script>




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

C#


    protected void btnOpenPop_Click(object sender, EventArgs e)


    {


        string url = "http://www.dotnetcurry.com";


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


    }




VB.NET


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


        Dim url As String = "http://www.dotnetcurry.com"


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


    End Sub




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

8 Responses to "Opening a new window from code behind on Button Click in ASP.NET"
  1. JeffMc said...
    March 19, 2009 3:32 PM

    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.

    Aloha,
    -Jeff

  2. JeffMc said...
    March 19, 2009 3:52 PM

    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 window.open. 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();

    window.open("YourPage.aspx?Param=" + result);
    }

    Aloha,
    -Jeff

  3. Suprotim Agarwal said...
    March 22, 2009 12:30 AM

    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 = window.open('some.htm','','width=100,height=100,left=0,top=0,scrollbars=no');
    if(pop)
    alert('Pop Up Enabled');
    }

  4. SVK said...
    April 8, 2009 10:56 PM

    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>window.open" & "('test.aspx?id=" & code.Value & " ','_new','width=800,height=800');/script")

    i have remove <> after and before the script word

  5. Jim said...
    March 12, 2010 3:19 PM

    Thanks, you solved my problem.

  6. .net developer said...
    September 24, 2010 12:13 PM

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

  7. Anonymous said...
    April 21, 2012 12:30 AM

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

  8. Anonymous said...
    May 21, 2012 7:30 PM

    Thanks for the help!!

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions