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?
You can use like this with Link Button
Replace
target="_blank"
toOnClientClick="window.document.forms[0].target='_blank';"
.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 useResponse.Redirect("~/AnotherPage.aspx")
to navigate to another page.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.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: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.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:
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:
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.