Feed on
Posts
Comments

To open a page in new window with ASP.NET 2.0 button PostBackURL property you have to set the FROM’s target to "_blank".

Page.Form.Target = "_blank";

Or to use the following JavaScript code:

<script type="text/javascript">
    var sFormID = ‘<%=Page.Form.ClientID%>’;
    var oForm = document.getElementById(sFormID);
    oForm.target = ‘_blank’;
</script>

,
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • dzone
  • Netscape
  • digg
  • YahooMyWeb
  • Technorati

7 Responses to “How to open PostBackURL in new window”

  1. on 01 Jun 2007 at 5:21 am Peter

    This is just what I have been looking for and use of the Page.Form.Target is what I would like to use. I am having a difficulty getting it to do just that though. Could you elaborate on how and where it should be used?

  2. on 01 Jun 2007 at 6:38 am Peter

    I figured it out. By placing it inside of the Page_Load and in the code segment that handles the PostBackURL the output was written to the new page. Briliant! And others thought I was crazy to ask how this could be done!

    Thanks again for your post.

  3. on 01 Jun 2007 at 7:04 am Peter

    So, I thought I had it but it appears that I still do not. If I place it in the “Page_Load” for the main page then a new page is created whenever I change a value in a control and cause a post back operation asside from the submit button press. If I place it within the submit button press it does not cause the page to open in a new tab/browser session. Have you managed to get the submit button to be the onl one that causes the new page to be created? I need the other controls to do the postback operation and only the button to cause the new page creation.

  4. on 02 Jun 2007 at 6:01 pm Ivan Uzunov

    Not sure what you mean by “the other controls to do the postback operation and only the button to cause the new page creation”?

    If you have more than one controls that do postback then you can use the JavaScript (code above). You can use it in a function called when the button is clicked and than submit the form using _doPostback JavaScript.

    Hope this helps :)

  5. on 04 Jun 2007 at 3:03 am Peter

    Ivan, Unfortunately no success with the way it was layed out above but I did with a couple of modifications. With the script you provided it does create a new page but it does so at every load. The architecture I am choosing is at odds with what your script, and possibly common sense, are trying to accomplish. I have a page “Report.aspx”, that when the user presses the submit button, will call itself and knows (through parameters) that it should now display the results of the report. I am reusing the same page for security authorization simplisity. Anyways, what I want is that when the user clicks on the submit button it would create a new page (that is generated as a post back for the “Report.aspx” page) with the results in it. What happens with the script you provided is that with every launch of the page I get a new instance.

    So, here is the simple modification that I made to your script and that I call from the submit button.

    function LoadNewPage(){
    var sFormID = ‘’;
    var oForm = document.getElementById(sFormID);
    oForm.target = ‘_blank’;
    }

    Note the use of “_blank” in place of “_self”.

    At any rate I want to thank you for the excellent work with your blog as it has saved me significant hair loss. :-)

  6. on 08 Jun 2007 at 3:02 pm Ivan Uzunov

    My mistake - I’ve fixed the “typo” in the post.

    Thanks Peter :)

  7. on 09 Jun 2007 at 6:33 pm Peter

    Ivan, I have made further progress with the solution and now it works wonderfully.

    The JavaScript is now:

    function LoadNewPage(){
    var sFormID = ‘’;
    var oForm = document.getElementById(sFormID);
    oForm.target = ‘MeterListReport’;
    }
    function LoadCurrentPage(){
    var sFormID = ‘’;
    var oForm = document.getElementById(sFormID);
    oForm.target = ‘’;
    }

    In my code I have on the front page the following controls:

    Then the only thing that really needed t be done is a small adjustment in the code for “Page_Load”. Because the submit button added the “action” parameter it sticks with any subsequent postbacks. This means that every time a user changes the drop down list selection it tries to run the action code. So in the “Page_Load” I added the following:

    Control control = null;
    string ctrlname = Page.Request.Params[”__EVENTTARGET”];
    if (ctrlname != null && ctrlname != String.Empty) {
    control = Page.FindControl(ctrlname);
    if (control != null)
    {
    if (control.ID.CompareTo(”MeterListsDropDown”) == 0)
    {
    action_code = 0;
    }
    }
    }

    Now all is wonderful in my life. Thank you so much for blogging your solution as it significantly helped me.

Trackback URI | Comments RSS

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a