Can you open a new window from a post back?

Rate It (3)

Last post 08-13-2009 3:23 AM by dhinakaranpc. 10 replies.

Sort Posts:

  • Can you open a new window from a post back?

    12-31-2003, 12:46 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    This question has been asked time and time again. When the server sends HTML to a browser, these two rules are followed:
    1. The browser must have requested the page via an HTTP GET or POST. (The server cannot change a page without the browser asking for it.)
    2. The browser will only retrieve the HTML into the window that requested it.

    There are only two ways to open a new window that I know about:
    1. The <a> tag includes a target= property specifying a new window. (The HyperLink webcontrol is simply a way to generate the <a> tag. It includes a Target property.)
    2. Use JavaScript such as a window.open or window.createPopup.

    The <a> tag opens the new window before posting back and requests the new HTML from that new window.

    The JavaScript technique requires that the original page already contains the JavaScript or when the server posts back, the browser is sent new HTML containing the new JavaScript to the original window. That means the original page's contents are replaced.

    If you want to use the JavaScript technique to open a new window on post back:
    1. Prepare the HTML necessary for the original page to draw.
    2. Use Page.RegisterStartupScript() to call window.open. This will allow the new window to popup as the original window is loaded.
    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Can you open a new window from a post back?

    02-04-2004, 5:11 AM
    • Member
      411 point Member
    • ariya
    • Member since 01-27-2004, 6:16 AM
    • tehran - iran
    • Posts 81
    hi all ,

    you can do it, just read this and you have done it !

    http://dotnetjohn.com/articles/articleid88.aspx

    ariya
  • Re: Can you open a new window from a post back?

    02-06-2004, 1:22 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    The article makes sense although it opens a window based on a click after the page reloads, not immediately on post back.
    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Can you open a new window from a post back?

    06-09-2008, 2:43 PM
    • Member
      2 point Member
    • aiasso
    • Member since 06-09-2008, 6:36 PM
    • Posts 1

    I found a way to do this using JavaScript.  I've tested it with the ImageButton control using ASP.NET 2.0.

    The approach is to add an onClick attribute to the tag that changes the form's target to the new target:

    MyImageButton.Attributes.Add("onClick","this.form.target='" & Target & "'")

    where Target is the target you want (eg. "_self","_top","PrintPreview", etc.)

    I would think this would also work for Hyperlinks, etc. as well, though I haven't tested it.
    Note the concatenation above uses ' quotes around the target name.  The output in HTML would look like:

     <input type="image" name="UserControlMenusAndToolbars1$UserControlToolbar1$ctl04" title="Print..." src="../images/print.gif" onclick="this.form.target='PrintPreview';" style="border-width:0px;" />

  • Re: Can you open a new window from a post back?

    06-21-2008, 6:24 AM
    • Member
      528 point Member
    • kathirvelmm
    • Member since 05-05-2008, 7:46 AM
    • Chennai-India
    • Posts 160
     
    1    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="FtpUpload.aspx.cs" Inherits="_Default" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml" >
    6    <head runat="server">
    7        <title>Untitled Page</title>
    8        <script type="text/javascript" language="javascript">
    9        window.close=tt();
    10       function tt()
    11       {
    12           window.open(href="http://192.168.16.30/swlc" ,height="200", width="200");
    13           }
    14       </script>
    15   </head>
    16   <body bgcolor="linen">
    17       <form id="form1" runat="server">
    18   </form>
    19   </body>
    20   </html>
    21   
    
     
    This has been tested and works.
     
    Kathirvel
    Follow me here
    Please mark as Answer if this solves your problem, Let other users get benefited.


  • Re: Can you open a new window from a post back?

    06-27-2008, 11:08 AM
    • Member
      118 point Member
    • CHolt
    • Member since 12-27-2006, 4:39 PM
    • Posts 185

    The only problem with using window.open in a startup script is that it will be blocked by a popup blocker

  • Re: Can you open a new window from a post back?

    08-07-2008, 10:30 AM
    • Member
      6 point Member
    • tirumal
    • Member since 08-07-2008, 2:02 PM
    • Posts 3

    To open a window from Parent window and to close a child window(Pagetobeopend.html), you can use following code

    <body onload="javascript: window.open('pagetobeopened.html','','');window.opener=''; window.close();">
    <a href="javascript: window.open("pagetobeopened.html");window.opener=''; window.close();">Close Main Window without Confirmation</a>

  • Re: Can you open a new window from a post back?

    06-13-2009, 8:48 AM
    • Member
      2 point Member
    • Trelane-
    • Member since 06-13-2009, 12:45 PM
    • Posts 1

    Top Marks, absolutly perfectly correct:

    this.myimagebutton.Attributes.Add("onClick", "this.form.target='_blank'"); //opens new window from image button and casues postback

    (C# equivilent)

    Mike
    N-Dimensional Ltd
    http://www.spyderhosting.co.uk
  • Re: Can you open a new window from a post back?

    08-11-2009, 1:24 AM
    • Member
      128 point Member
    • dhinakaranpc
    • Member since 07-14-2009, 9:04 AM
    • Posts 33


    We can call the javascript function in the body tag which is called every time when the page loads.


    <html>
    <head>
    <script type="text/javascript">


    function  OpenPopupWindow()
    {

       window.open('../HelpDocs/AdmStudCenterwiseRpt.html','HelpDoc','width=600,resizable=yes,scrollbars = yes');    

    }


    </script>
    </head>

    <body onload="OpenPopupWindow()">

    </body>
    </html>

  • Re: Can you open a new window from a post back?

    08-11-2009, 7:43 AM
    • Participant
      863 point Participant
    • nitindhiman
    • Member since 12-27-2007, 4:41 PM
    • chandigarh, india
    • Posts 144

    Hi PLBlum,

    I rarely need such kind of functionality but it is a gud topic, I agree with you that on each n every request the browser gets a new HTML based on the request made by that because HTTP is a stateless protocol n can not mentain the state.

    And as you are saying that we can use registerClientstartUpScript to regester a script while page is loading, I hv 2 question for you, can't we perform the same functionality on onLoad event of the window, Like:

    window.onload = function()

    {

    alert('hello');

    }

    and 2nd 1 is wt is the difference between registerclientstartUpScript and Response.Write.


    Thnx...

    Nitin Dhiman....!!!!
    Please Mark Answer If Helped....!!!!
  • Re: Can you open a new window from a post back?

    08-13-2009, 3:23 AM
    • Member
      128 point Member
    • dhinakaranpc
    • Member since 07-14-2009, 9:04 AM
    • Posts 33

    When we use update panel in the page the response.write wont  support,  where the other will support. This could be a one of the reason.

Page 1 of 1 (11 items)