4

I'm trying to open a page in new tab/window on button click.I tried in the google got this code but its not working.

Can anybody help me with this?

<asp:Button ID="btn" runat="Server" Text="SUBMIT" 
     OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void btnNewEntry_Click(object sender, EventArgs e)
{
    Response.Redirect("CMS_1.aspx");
}

When I use this I'm getting error saying

   Microsoft JScript runtime error: 'aspnetForm' is undefined.
Carls Jr.
  • 3,088
  • 7
  • 37
  • 57
Naresh
  • 657
  • 3
  • 16
  • 35
  • What is your form's ID? Have you tried to change the "aspnetForm" in `OnClientClick="aspnetForm.target..."` to the id of your `
    `?
    – JMD Jan 30 '15 at 15:56

6 Answers6

13

You can do something like this :

<asp:Button ID="Button1" runat="server" Text="Button"
    onclick="Button1_Click" OnClientClick="document.forms[0].target = '_blank';" />
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
satan singh
  • 141
  • 1
  • 2
8

Wouldn't you be better off with

<asp:HyperLink ID="HyperLink1" runat="server" 
            NavigateUrl="CMS_1.aspx" 
            Target="_blank">
    Click here
</asp:HyperLink>

Because, to replicate your desired behavior on an asp:Button, you have to call window.open on the OnClientClick event of the button which looks a lot less cleaner than the above solution. Plus asp:HyperLink is there to handle scenarios like this.

If you want to replicate this using an asp:Button, do this.

<asp:Button ID="btn" runat="Server" 
        Text="SUBMIT"
        OnClientClick="javascript:return openRequestedPopup();"/>

JavaScript function.

var windowObjectReference;

function openRequestedPopup() {
    windowObjectReference = window.open("CMS_1.aspx",
              "DescriptiveWindowName",
              "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
}
naveen
  • 53,448
  • 46
  • 161
  • 251
4

I think your code should work just remove one thing from here but it'll do redirection from current page within existing window

<asp:Button ID="btn" runat="Server" Text="SUBMIT" 

 OnClick="btnNewEntry_Click"/>    



protected void btnNewEntry_Click(object sender, EventArgs e)
 {
    Response.Redirect("CMS_1.aspx");
 }

And if u wanna do the this via client side scripting Use this way

<asp:Button ID="BTN" runat="server" Text="Submit" OnClientClick="window.open('Default2.aspx')" />

According to me you should prefer the Client Side Scripting because just to open a new window server side will take a post back and that will be useless..

Syeda
  • 1,215
  • 11
  • 23
  • It's opening the window in new window.Can I do this in new tab. – Naresh Jun 06 '11 at 09:15
  • the signature of `window.open` is `var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);` that means windowName should also be passed to `window.open` https://developer.mozilla.org/En/Window.open – naveen Jun 06 '11 at 09:21
  • 1
    To open a pop up in a tab, you should set how the pop up's are handled in browser. Ex : in IE8+, Go to internet options, General, Tabs section, set when pop up's are encountered section – Nayana Setty Jan 25 '12 at 08:44
2

This is what I ended up using. Temporarily sets target to _blank, then sets it back.

OnClientClick="var originalTarget = document.forms[0].target; document.forms[0].target = '_blank'; setTimeout(function () { document.forms[0].target = originalTarget; }, 3000);"
kernelk
  • 368
  • 2
  • 8
1

You have to add following in header:

<script type="text/javascript">
        function fixform() {
            if (opener.document.getElementById("aspnetForm").target != "_blank") return;

            opener.document.getElementById("aspnetForm").target = "";
            opener.document.getElementById("aspnetForm").action = opener.location.href;
            }
</script>

Then call fixform() in load your page.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
1

You can use Rout redirecting.

protected void btnNewEntry_Click(object sender, EventArgs e)
{
    Response.RedirectToRoute("CMS_1"); 
}

which requires to define your routing logic in Global.asax file that could be like that:

routes.MapPageRoute("CMS_1", "CMS_1", "~/CMS_1.aspx");

where any request by CMS_1 pattern in application scope will be redirecting to CMS_1.aspx, but in URL shows like www.yoursite.com/CMS_1

Shaahin
  • 1,195
  • 3
  • 14
  • 22