C# Target="_blank" in a LinkButton
Asked Answered
F

9

17

is it possible to have a target="_blank" in LinkButton...mine doesnt seem to be working

<asp:LinkButton runat="server" ID="g31" Text="PDF" 
    CommandArgument='<%# DataBinder.Eval(Container.DataItem,"productID") %>'
    CommandName='<%# DataBinder.Eval(Container.DataItem,"documentID") %>'
    OnCommand="linkbutton_showpdf" target="_blank">
</asp:LinkButton>

Or would I have to use a different button format?

Fiona answered 13/2, 2012 at 4:0 Comment(4)
I have a Response.Redirect in my code behind...I am trying to open it in another window.Fiona
I might be missing it, but I don't see any sign of target="_blank".Anchusin
You may get the answer here: #2637587Repent
The LinkButton acts like a Button control. Would you expect it to be able to facilitate this behavior? If it is absolutely necessary either use javascript or a hyperlink.Tackle
A
-3

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>
Amative answered 13/2, 2012 at 4:14 Comment(1)
This is not correct for asp.net. See my answer below.Lory
H
36

You can use like this with Link Button

Replace target="_blank" to OnClientClick="window.document.forms[0].target='_blank';".

Hereinafter answered 9/10, 2012 at 12:33 Comment(3)
I made a mistake... it's w and not W. Thank you. It's working nowOza
This will make every link within that form "target=_blank" after the linkbutton is clickedNightclub
you need to add setTimeout(function () { window.document.forms[0].target = ''; }, 0);Crowl
L
22

None of the current answers are correct, even the <a> tag is not the correct answer in asp.net.

Use the HyperLink Button. There is even a property for the target attribute.

<asp:HyperLink runat="server" 
    NavigateUrl='http://rrs.com/aspx/Equipment/EquipmentType.aspx'
    Target="_blank">
        Create/Edit Part Types
</asp:HyperLink>
Lory answered 5/12, 2014 at 15:53 Comment(0)
K
7

You can use the Hyperlink control, which does have a target='_blank' property. However if you must use a LinkButton control, then you can add a OnClientClick attribute which then calls a JavaScript function to open a popup window

window.open();
Knudsen answered 13/2, 2012 at 4:10 Comment(0)
P
5

I was working with Devsaninii's answer where I changed the target of the form and found the undesired behavior of the rest of my pages switching to a new window after clicking a link that changed the target. Which made sense but was undesirable.

I was opening files with some links and loading new pages with others. I wanted the files to open in new windows and I wanted the new pages to open in the same window. But after I changed the target everything was in a new window. I could have gone through and added a client click handler to each and every linkbutton, but that was too cumbersome.

So here is what I came up with:

I added a class to my linkbuttons that were supposed to have a new window as the target and then I added this little piece of jQuery to my script:

$(function() {
    $('a').click(function() {
        if ($(this).hasClass('changeTarget')) {
            window.document.forms[0].target = '_blank';
        } else {
            window.document.forms[0].target = '_self';
        }
    });
});

Now when a linkbutton that should have a new window is pressed, it opens in a new window, and when anything else is pressed, it opens in the same window.

Polash answered 18/9, 2013 at 14:32 Comment(0)
I
1

A LinkButton in ASP.NET web forms just submits the underlying form using JavaScript so the target="_blank" doesn't actually do anything. To open a new window you can instead modify the target property of the "form" via JavaScript using the LinkButton's OnClientClick event. You also need to undo the modification after the click (using a setTimeout) otherwise further button clicks will unintentionally target a new tab.

<asp:LinkButton ID="uiNewTabExample" Text="Open New Tab" OnClick="uiNewTabExample_Click" OnClientClick="window.document.forms[0].target = '_blank'; setTimeout(function () { window.document.forms[0].target = '' }, 0);" 
runat="server" />
Itacolumite answered 3/6, 2019 at 16:40 Comment(0)
M
0

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

<asp:LinkButton 
        ID="btNext" 
        runat="server" 
        CssClass="btn btn-sm default next"
        OnClientClick="aspnetForm.target ='_blank';" 
        OnClick="ibtNext_Click">
        Next
</asp:LinkButton>
Morrill answered 24/5, 2022 at 5:41 Comment(0)
F
-1

After looking at these answers and none was exactly what I wanted (do this with a button look), I ended up using a hyperlink control, but used the same css style as my regular buttons:

.button {

    background-color:#011745;
    color:white;
    padding:7px 12px 7px 12px;
    margin:3px;
    border-style:none;
    font-size:12px;

}

.button:hover {

    background-color:#336699;
    color:white;
    padding:7px 12px 7px 12px;
    margin:3px;
    border-style:none;
    font-size:12px;

}

It looked just like them! If you are going for a button that opens a link in a new window, which I was, it was almost perfect. In a set of table cells, it displayed just a touch lower than the regular buttons, so I styled it like this: "position:relative; top:-2px;" and that did the trick. I also had to force the forecolor white:

<asp:HyperLink ID="btnSummaryReport" Target="_blank" runat="server" Text="SUMMARY REPORT" CssClass="button" ForeColor="white" Font-Size="8" style="position:relative; top:-2px" />
Fluxion answered 15/12, 2015 at 21:52 Comment(0)
P
-1

Adding to @Devsainii answer above.

Add the attribute OnClientClick="window.document.forms[0].target='_blank';" to your LinkButton and then in the code behind, just use Response.Redirect("~/AnotherPage.aspx") to navigate to another page.

Premer answered 14/11, 2017 at 21:20 Comment(0)
A
-3

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>
Amative answered 13/2, 2012 at 4:14 Comment(1)
This is not correct for asp.net. See my answer below.Lory

© 2022 - 2024 — McMap. All rights reserved.