2

I have added a button on my asp page and I am executing some process at server side then redirecting a link to another page.

What I am doing in my page is that I am calling a method On asp:Button OnClientClick event where I am assigning a aspnetForm.target='_blank' to navigate the page in different tab.

ASP.NET CODE:

<asp:Button ID='btn' runtat='server' OnClientClick='ShowPage();' Text='Show Page' />

JavaScript CODE:

function ShowPage() {
    aspnetForm.target = '_blank';
}

I am using using Response.Redirect() method to navigate a page. This method is called only after verifying required validations at server side.

C#.NET Code

If (ValidData())
{
   Response.Redirect('myPage.aspx?preview=true', True);
}

The validation code is huge and there are also some further process are being executed during the validation. So, I cannot use WebMethod for ajax call.

Everything is working correctly but, the problem is that there are some other links and buttons also. So, when i click on any of them the page is also redirecting for that particular request and I don't want to do that. It happens because the `aspnetForm.target='_blank`` is still exist in the asp form and it will perform same action for other links also.

My requirement is that the page should be navigated in new tab only when a particular button is pressed. other navigation process should be done is same page. I am wondering that is there any method that i can use to remove target = '_blank' after postback.

I cannot use rel='external' because I am not using anchor<a> tag. I am using asp:Button which is also handling some server side events.

Or is there any other way that i can use to navigate the page in new tab without using target='_blank' method?

EDITED:

Currently I am using Page.ClientScript.RegisterStartupScript() to execute my Popup window script. But, when the popup blocker is on it doesn't work. So, I have to chose another way.

Page.ClientScript.RegisterStartupScript(Me.GetType(), "OpenWindow", "window.open('myPage.aspx?preview=true','_newtab');", True)
Shell
  • 6,818
  • 11
  • 39
  • 70
  • The one's you want to navigate to new tab put `target="_blank"` on them and don't put target at all on one's you want to navigate in same parent page. Is that makes sense or did i missed something magical here?? – Rohit416 May 07 '15 at 06:16
  • yes, your logic will work but, it would be painful to handle all controls' `OnClientClick` event when there are so much links and buttons placed in the page. – Shell May 07 '15 at 06:21
  • ah, `window.open()` would be painful as it behaves weirdly sometimes. Yes you are right it won't be easy because browsers open pages in new tabs if the request comes from a trusted source, if we try to do it manually then i don't think it will work as expected throughout. However you can try `window.open(url, '_blank')` or check [this link.](http://stackoverflow.com/questions/23101713/how-to-open-a-link-in-new-tab-using-javascript) – Rohit416 May 07 '15 at 06:56
  • Yes, I have also tried that and `_newtab` as well but it shows same popup blocker message. I am redirecting page from server side and I am using `asp:Button` to trigger it. So, I cannot give the target tag to my element as suggested in accepted answer. I have also tried ajax call with `async: false`. but, still it shows same message. – Shell May 07 '15 at 07:21
  • After post back instead of removing `target=_blank` why don't you try to set it to another value like `target=_self` – Shoaib Shakeel May 07 '15 at 07:33
  • @ShoaibShakeel I don't know where should I put that code to set the `target=_self` after postback. I am setting `target=_blank` in `OnClientClick` event of `asp:Button` – Shell May 07 '15 at 08:38

1 Answers1

4

I have solve my issue by using setTimeout() method. I am resetting form attribute target to _self after 1000 milliseconds.

Javascript Code:

function ShowPage() {
    aspnetForm.target = "_blank";
    setTimeout(function() {
        aspnetForm.target = "_self";
    }, 1000);
}

ASP.NET Code

<asp:Button ID='btn' runtat='server' OnClientClick='ShowPage();' Text='Show Page' />

And, I can use my existing method Response.Redirect() which will not trigger the Popup Blocker message.

It will reset the target attribute of form to _self after navigating new tab.

Shell
  • 6,818
  • 11
  • 39
  • 70