首页 > 解决方案 > asp.net LinkBut​​ton OnClick 未使用 href 触发

问题描述

我有一个 LinkBut​​ton,它工作得非常好,但是当我向 LinkBut​​ton 添加一个 href 时,它会将我重定向到链接而不触发 OnClick 函数。

<asp:LinkButton target="_blank" ID="ad_main_form"  
    OnClick="ad_button_Click" runat="server"  
    CssClass="ad_main_panel" CausesValidation="False" 
    href="https://stackoverflow.com">
</asp:LinkButton>

如何同时触发 OnClick 和 href?

标签: asp.netonclickhreflinkbutton

解决方案


LinkButton has a property with name PostbackUrl instead using href please use this property.

To avoid all this hassle what I would have done is, call a Response.Redirect('url') inside my click handler so it does both the jobs in a cleaner manner.

Update
Unfortunately you cannot open in new tab using Response.Redirect() method, but you can certainly write javascript code to open the url in a new tab. See:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openurl", "window.open('http://www.mywebsite.com' ,'_blank');", true);

Alternatively, you can simply do it like this:

Response.Write("<script>window.open('http://www.mywebsite.com', '_blank')</script>");

推荐阅读