LinkButton 中的 C# Target="_blank"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9255427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Target="_blank" in a LinkButton
提问by user979331
is it possible to have a target="_blank"in LinkButton...mine doesnt seem to be working
是否有可能target="_blank"在LinkButton...我的似乎没有工作
<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?
还是我必须使用不同的按钮格式?
采纳答案by ShankarSangoli
Just render an anchor with href set to appropriate url and set the targetattribute to _blankit will open the url into new window.
只需渲染一个带有 href 设置为适当 url 的锚点并将target属性设置为_blank它就会将 url 打开到新窗口中。
<a href="urlOfThePage" target="_blank" >Click me</a>
回答by skub
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 OnClientClickattribute which then calls a JavaScript function to open a popup window
您可以使用具有target='_blank'属性的 Hyperlink 控件。但是,如果您必须使用 LinkButton 控件,那么您可以添加一个OnClientClick属性,然后调用 JavaScript 函数来打开一个弹出窗口
window.open();
回答by Devsainii
You can use like this with Link Button
您可以像这样使用链接按钮
Replace target="_blank"to OnClientClick="window.document.forms[0].target='_blank';".
替换target="_blank"为OnClientClick="window.document.forms[0].target='_blank';"。
回答by Pow-Ian
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.
我正在使用 Devsaninii 的答案,在那里我更改了表单的目标,并发现我的其余页面在单击更改目标的链接后切换到新窗口的不良行为。这是有道理的,但却是不可取的。
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.
我正在打开带有一些链接的文件,并与其他人一起加载新页面。我希望文件在新窗口中打开,我希望新页面在同一窗口中打开。但是在我更改目标后,一切都在一个新窗口中。我本可以通过并为每个 . 添加一个客户端点击处理程序linkbutton,但这太麻烦了。
So here is what I came up with:
所以这就是我想出的:
I added a class to my linkbuttonsthat were supposed to have a new window as the target and then I added this little piece of jQuery to my script:
我向我添加了一个类,linkbuttons它应该有一个新窗口作为目标,然后我将这一小块 jQuery 添加到我的脚本中:
$(function() {
$('a').click(function() {
if ($(this).hasClass('changeTarget')) {
window.document.forms[0].target = '_blank';
} else {
window.document.forms[0].target = '_self';
}
});
});
Now when a linkbuttonthat 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.
现在,当linkbutton按下应该有一个新窗口的 a 时,它会在一个新窗口中打开,而当按下其他任何东西时,它会在同一窗口中打开。
回答by M H
None of the current answers are correct, even the <a>tag is not the correct answer in asp.net.
当前的答案都不是正确的,甚至<a>标签也不是 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>
回答by Steve
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:
查看这些答案后,没有一个是我想要的(用按钮外观来做),我最终使用了一个超链接控件,但使用了与我的常规按钮相同的 css 样式:
.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:
它看起来就像他们!如果你想要一个在新窗口中打开链接的按钮,我就是这样,它几乎是完美的。在一组表格单元格中,它的显示比常规按钮低一点,所以我将它的样式设置为:“位置:相对;顶部:-2px;” 这就是诀窍。我还不得不强制前色为白色:
<asp:HyperLink ID="btnSummaryReport" Target="_blank" runat="server" Text="SUMMARY REPORT" CssClass="button" ForeColor="white" Font-Size="8" style="position:relative; top:-2px" />
回答by R.S.K
Adding to @Devsainii answer above.
添加到上面的@Devsainii 答案。
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.
将该属性添加OnClientClick="window.document.forms[0].target='_blank';"到您的 LinkButton,然后在后面的代码中,仅用于Response.Redirect("~/AnotherPage.aspx")导航到另一个页面。
回答by WDuffy
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.NET Web 表单中的 LinkButton 只是使用 JavaScript 提交底层表单,因此 target="_blank" 实际上不执行任何操作。要打开一个新窗口,您可以使用 LinkButton 的 OnClientClick 事件通过 JavaScript 修改“表单”的目标属性。您还需要在单击后撤消修改(使用 setTimeout),否则进一步单击按钮将无意中定位到新选项卡。
<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" />

