ASP.NET - Open page on new tab
Asked By stefan on 11-Aug-11 04:03 AM
Hi all i am writting a website and cannot get it right to open a new page on a new tab. Can anyone plz help me with this as it is quite urgent
Thanks in advance
Stefan
Jitendra Faye replied to stefan on 11-Aug-11 04:08 AM
Try this code-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="hyperlinkDemo.aspx.cs" Inherits="hyperlinkDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>hyperlink Demo</title>
<script language="javascript" type="text/javascript">
function openNewWindows(){
window.open("http://www.asp.net");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HyperLink ID="hlDemo" runat="server" onclick="openNewWindows()" style="cursor:pointer; text-decoration:underline;">HyperLink Demo </asp:HyperLink>
</div>
</form>
</body>
</html>
Let me know.
Riley K replied to stefan on 11-Aug-11 04:24 AM
Here is the correct code for you
If you use Window.open() it will open in new window
<asp:Button ID=”btnNewEntry” runat=”Server” CssClass=”button” Text=”New Entry” OnClick=”btnNewEntry_Click” OnClientClick=”aspnetForm.target =’_blank’;”/> // aspnetForm.target =’_blank’ will add handler to open new tab
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect(”New.aspx”);
}
Try and let me knwo
dipa ahuja replied to stefan on 11-Aug-11 04:26 AM
Every link you create in markup you will get the option in of open in new tab, but for imagebutton, linkbutton etc your wont get option for open in new tab:
for ex:
<%--Get Option of new tab--%>
<a href="fb.aspx">Click</a>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/fb.aspx" Text="Click" />
<%--No Option of new tab--%>
<asp:Button ID="Button2" runat="server" Text="Click"/>
<asp:LinkButton ID="LinkButton1" runat="server" Text="click" PostBackUrl="~/fb.aspx"/>
<asp:ImageButton ID="ImageButton1" ImageUrl="~/1.bmp"
runat="server" PostBackUrl="~/fb.aspx" Height="100" Width="50" />
Venkat K replied to stefan on 11-Aug-11 04:37 AM
I don't think we can control opening the page in new tab or new windows using serverside code. For this you need to use javascript.
Secondly, i think you can't open the page in new tab because there is no such option in javascript AFAIK.
This is a option in IE when a new page opened, you can select either you need to open this in new tab / window:
Check this option in IE:
http://www.ehow.com/how_5804941_make-pages-open-new-tabs.html
Thanks
Anoop S replied to stefan on 11-Aug-11 04:45 AM
http://www.456bereastreet.com/archive/200605/using_javascript_instead_of_target_to_open_new_windows/
http://forums.asp.net/t/1207885.aspx/1?make+hyperlink+open+in+new+tab
Hope the above links will help.
Radhika roy replied to stefan on 11-Aug-11 11:26 AM
just insert target="_blank" in that tag like this:
<asp:HyperLink ID="hyp" NavigateUrl="~/aboutus.aspx" runat="server" Text="click" Target="_blank"></asp:HyperLink>