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") %>'  />

8 Answers 8

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.
1
  • 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 Commented Apr 15, 2010 at 7:34
22

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")
4
  • I would like use it in asp:TemplateField in GridView.
    – Kiquenet
    Commented Jun 12, 2015 at 5: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"). Commented Jun 13, 2015 at 6:06
  • Make this comment as your answer instead.
    – Jam
    Commented Aug 28, 2015 at 6:01
  • I think this is the best solution. Detailed and to the point!
    – sohaiby
    Commented Oct 27, 2015 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") %>'  />
0
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>
1
  • 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. Commented Nov 10, 2016 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'
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.

3
  • Mr. Daniel Dyson the code which you given lbnkVidTtile1.Enabled = true; is place in link button click event? Commented Apr 14, 2010 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? Commented Apr 14, 2010 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) Commented Apr 15, 2010 at 7:25
0

It throws error.

Microsoft JScript runtime error: 'aspnetForm' is undefined

-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>
2
  • 2
    Hmm, That does render the mark up as you state, but opens a blank window, and doesn't perform the PostBack, so hardly ideal ;) Commented Apr 14, 2010 at 12:22
  • 1
    It did not work for me. as I am using link button in Grd view
    – BJ Patel
    Commented Mar 26, 2013 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.