13

I have an asp.net application, where the user would click a button and launch another page (within the same application). The issue I am facing is that the original page and the newly launched page should both be launched.

I tried response.redirect, but that tends to unload the original page.

Any suggestions?

Csharp
  • 2,722
  • 15
  • 46
  • 77

5 Answers5

32

This button post to the current page while at the same time opens OtherPage.aspx in a new browser window. I think this is what you mean with ...the original page and the newly launched page should both be launched.

<asp:Button ID="myBtn" runat="server" Text="Click me" 
     onclick="myBtn_Click" OnClientClick="window.open('OtherPage.aspx', 'OtherPage');" />
Claudio Redi
  • 63,880
  • 13
  • 118
  • 146
15

Edited and fixed (thanks to Shredder)

If you mean you want to open a new tab, try the below:

protected void Page_Load(object sender, EventArgs e)
{
    this.Form.Target = "_blank";
}

protected void Button1_Click(object sender, EventArgs e)
{

    Response.Redirect("Otherpage.aspx");
}

This will keep the original page to stay open and cause the redirects on the current page to affect the new tab only.

-J

KreepN
  • 8,380
  • 1
  • 37
  • 56
  • 1
    Works as of the edit. I undeleted in case people were looking for an alternative to the solutions above. – KreepN Sep 09 '11 at 19:38
  • Not great if your form has multiple buttons. The "_blank" line will cause a duplicate page to be opened every time a button is clicked. – Zizzipupp Dec 03 '19 at 14:57
  • 1
    @Zizzipupp Indeed. That was mentioned 8 years ago in Nick Rolando's comment chain in the answer below this one. – KreepN Dec 03 '19 at 17:06
5

If you'd like to use Code Behind, may I suggest the following solution for an asp:button -

ASPX Page

<asp:Button ID="btnRecover" runat="server" Text="Recover" OnClick="btnRecover_Click" />

Code Behind

    protected void btnRecover_Click(object sender, EventArgs e)
    {
        var recoveryId = Guid.Parse(lbRecovery.SelectedValue);
        var url = string.Format("{0}?RecoveryId={1}", @"../Recovery.aspx", vehicleId);

        // Response.Redirect(url); // Old way

        Response.Write("<script> window.open( '" + url + "','_blank' ); </script>");
        Response.End();
    }
Riaan de Lange
  • 3,262
  • 4
  • 28
  • 34
1

You should use:

protected void btn1_Click(object sender, EventArgs e)
{
    Response.Redirect("otherpage.aspx");
}
Angelos Chalaris
  • 5,882
  • 7
  • 44
  • 65
Naqeeb Ahmed
  • 748
  • 5
  • 21
1

Use an html button and javascript? in javascript, window.location is used to change the url location of the current window, while window.open will open a new one

<input type="button" onclick="window.open('newPage.aspx', 'newPage');" />

Edit: Ah, just found this: If the ID of your form tag is form1, set this attribute in your asp button

OnClientClick="form1.target ='_blank';"
Nick Rolando
  • 25,176
  • 13
  • 72
  • 111
  • 1
    My 2nd (edited) solution will probably cause your postback results to be displayed in the new browser, while the old window won't change. If you looking to post to the current window and open a new one, go with Claudio's answer – Nick Rolando Sep 09 '11 at 19:23
  • 1
    Thanks for the catch on my solution there. It seems as though it used to work back when I used it on one of my sites, but has since then stopped functioning that way. I'm gonna have to change my code because of you :). – KreepN Sep 09 '11 at 19:28
  • 1
    @KreepN I was just messin around with your code, and it seems to work if `Form.Target = "_blank";` is read from their previous request. It sets up their next request to be responded in a new window. For ex: set it in Page_Load and all postbacks to this page will open in a new window ^^ – Nick Rolando Sep 09 '11 at 19:31