18

In my application I have some link buttons there but when I right click on them I cannot (they are in disable mode) find the menu items Open in new tab or Open in new window.

How do I show those menu items?

Code example:

<asp:LinkButton id="lbnkVidTtile1" runat="Server" CssClass="bodytext" Text='<%#Eval("newvideotitle") %>'  />
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

8 Answers8

23

From the docs:

Use the LinkButton control to create a hyperlink-style button on the Web page. The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.

As this isn't actually performing a link in the standard sense, there's no Target property on the control (the HyperLink control does have a Target) - it's attempting to perform a PostBack to the server from a text link.

Depending on what you are trying to do you could either:

  1. Use a HyperLink control, and set the Target property
  2. Provide a method to the OnClientClick property that opens a new window to the correct place.
  3. In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • can i get that in link button because i am passing some values using commandargument, it is not in hyperling even click event is also not there for hyperlink. Mr. Zhaph- Ben – Surya sasidhar Apr 15 '10 at 07:34
20

Here is your Tag.

<asp:LinkButton ID="LinkButton1" runat="server">Open Test Page</asp:LinkButton>

Here is your code on the code behind.

LinkButton1.Attributes.Add("href","../Test.aspx")
LinkButton1.Attributes.Add("target","_blank")

Hope this will be helpful for someone.

Edit To do the same with a link button inside a template field, use the following code.

Use GridView_RowDataBound event to find Link button.

Dim LB as LinkButton = e.Row.FindControl("LinkButton1")         
LB.Attributes.Add("href","../Test.aspx")  
LB.Attributes.Add("target","_blank")
Sikandar Amla
  • 1,415
  • 18
  • 27
  • I would like use it in asp:TemplateField in GridView. – Kiquenet Jun 12 '15 at 05:56
  • Yes that can be done. Add LinkButton1 into your template field. Use GridView_RowDataBound event to find Link button - Dim LB as LinkButton = e.Row.FindControl("LinkButton1") and LB.Attributes.Add("href","../Test.aspx") and LB.Attributes.Add("target","_blank"). – Sikandar Amla Jun 13 '15 at 06:06
  • Make this comment as your answer instead. – Jamil Aug 28 '15 at 06:01
  • I think this is the best solution. Detailed and to the point! – sohaiby Oct 27 '15 at 13:17
16

try by Adding following onClientClick event.

OnClientClick="aspnetForm.target ='_blank';"

so on click it will call Javascript function an will open respective link in News tab.

<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" CssClass="bodytext" Text='<%# Eval("newvideotitle") %>'  />
BJ Patel
  • 6,148
  • 11
  • 47
  • 81
5

This is not perfect, but it works.

<asp:LinkButton id="lbnkVidTtile1" runat="Server" 
    CssClass="bodytext" Text='<%# Eval("newvideotitle") %>'
    OnClientClick="return PostToNewWindow();"  />

<script type="text/javascript">
function PostToNewWindow()
{
    originalTarget = document.forms[0].target;
    document.forms[0].target='_blank';
    window.setTimeout("document.forms[0].target=originalTarget;",300);
    return true;
}
</script>
humbads
  • 3,252
  • 1
  • 27
  • 22
  • I prefer this solution because it sets the timeout of this target trick. If the timeout is not set, every link clicked after this point will open in a new window. – kerbasaurus Nov 10 '16 at 14:28
1
  1. LinkButton executes HTTP POST operation, you cant change post target here.
  2. Not all the browsers support posting form to a new target window.
  3. In order to have it post, you have to change target of your "FORM".
  4. You can use some javascript workaround to change your POST target, by changing form's target attribute, but browser will give a warning to user (IE Does), that this page is trying to post data on a new window, do you want to continue etc.

Try to find out ID of your form element in generated aspx, and you can change target like...

getElementByID('theForm').target = '_blank' or 'myNewWindow'
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
0

When the LinkButton Enabled property is false it just renders a standard hyperlink. When you right click any disabled hyperlink you don't get the option to open in anything.

try

lbnkVidTtile1.Enabled = true;

I'm sorry if I misunderstood. Could I just make sure that you understand the purpose of a LinkButton? It is to give the appearance of a HyperLink but the behaviour of a Button. This means that it will have an anchor tag, but there is JavaScript wired up that performs a PostBack to the page. If you want to link to another page then it is recommended here that you use a standard HyperLink control.

Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73
  • Mr. Daniel Dyson the code which you given lbnkVidTtile1.Enabled = true; is place in link button click event? – Surya sasidhar Apr 14 '10 at 12:21
  • PageLoad event in the code behind. You shouldn't have to do this because the default is true. Can you explain how the linkbuttons have been disabled? – Daniel Dyson Apr 14 '10 at 12:50
  • Mr. Daniel Dyson u not understand my question i think. when we open any site and there may be a link button when we right click on it and select the 'open link new tab' that is i need in my link button. present my link buttons are not getting that one (open Link new tab) – Surya sasidhar Apr 15 '10 at 07:25
0

It throws error.

Microsoft JScript runtime error: 'aspnetForm' is undefined

Lala
  • 31
  • 5
-4
 <asp:LinkButton ID="LinkButton1" runat="server" target="_blank">LinkButton</asp:LinkButton>

Use target="_blank" because It creates anchor markup. the following HTML is generated for above code

<a id="ctl00_ContentPlaceHolder1_LinkButton1" target="_blank" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton1','')">LinkButton</a>
Brij
  • 6,086
  • 9
  • 41
  • 69