1

I am trying to right click on a LinkButton and open it in a new tab or seperate window page displays nothing. I found some solution for the use of Hyperlink button as it has property to set target to "_blank" but LinkButton has not any target attribue.

I want to use LinkButton instead of hyperlink button, its just because i can't set command arguement or command name on hyperlink button and can't fire an event on it.

<asp:LinkButton ID="lnkHeadingHindi" Text='<%#Patrika.Common.ConvertNews(Eval("strMainHeadingHin").ToString())%>' CommandArgument='<%#Eval("intNewsId") %>' runat="server"></asp:LinkButton>

It would be great if anybody has a solution and let me know in case of any concern.

Thanks !!

Vedank Kulshrestha
  • 220
  • 1
  • 6
  • 23

5 Answers5

1

Based on the accepted answer to this question, if you want to perform a POST (such as a LinkButton does) but have the result open in a new window, you need to add target="_blank" to the form on the page, not the link itself.

Obviously you don't want to do this when you initially render the page, as everything that caused a postback would open in a new window.

Instead, try adding the following attribute to your LinkButton:

OnClientClick="$('form').attr('target', 'blank')"

This will dynamically set the form attribute just before the form is posted back by a click to the link.

Note that this doesn't give the right-click functionality you want, but it does work to open in a new window on a left-click.

If you don't have access to JQuery, you'll need to do something like

protected void Page_PreRender(object se, EventArgs e)
{
    this.Page.Form.ID = "someUniqueID"; // unless your form already has an ID
    yourLinkButton.OnClientClick =
        "document.getElementById('" +
        this.Page.Form.ClientID +
        "').setAttribute('target', '_blank')";
}
Community
  • 1
  • 1
Rawling
  • 49,248
  • 7
  • 89
  • 127
0

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.

If you like when click the link button,then open new window,

please see this

0

insert base tag like this

 <head>
    <base target="_blank" />
    </head>
Nikhil D
  • 2,479
  • 3
  • 21
  • 41
  • This doesn't work (for me at least) - the reference on the rendered anchor is `javascript:__doPostBack('ctl00$FeaturedContent$ctl00','')` so you end up trying to call a function in the new window that doesn't exist. – Rawling Mar 06 '13 at 08:32
0

If you want to pass some information to the page using hyperlink, pass it in the url using QueryString.

<asp:HyperLink id="hyperlink1" 
                  NavigateUrl="~/MyPage.aspx?intNewsId=10"
                  Text="ClickMe"
                  Target="_blank"
                  runat="server"/>    
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
0

You can't have it both ways, you'll have to choose between:

1) the linkbutton executing some code in the current page

2) a hyperlink opening another page in a new window

I guess you want to have the linkbutton executing some code in the new page, but that's impossible.

or if u like use . anger tag

Just render an anchor with href set to appropriate url and set the target attribute to _blank it will open the url into new window

<a href="urlOfThePage" target="_blank" >Click me</a>