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


9 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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');
    }

    ReplyDelete
  4. 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

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

    ReplyDelete
  6. Thanks for the help!!

    ReplyDelete
  7. I clicked ".net developer"'s link above, but it was 401. Searched his site, this is the new hideout for the article: http://www.yaplex.com/asp-net/open-new-window-from-code-behind-in-asp-net/

    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="window.open(self.location);" />

    ReplyDelete