简体   繁体   English

如何将JavaScript添加到ASP.NET超链接控件

[英]How to add javascript to ASP.NET hyperlink control

How can I enter this URL into a HyperLink ASP.NET Control ? 如何将该URL输入到HyperLink ASP.NET控件中?

<a href="https://plus.google.com/share?url={URL}" 
   onclick="javascript:window.open(this.href,'','menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;">
    <img src="https://www.gstatic.com/images/icons/gplus-64.png" alt="Share on Google+"/>
</a>

Just use onclick . 只需使用onclick Any properties that are not recognized by ASP.Net are passed as-is. ASP.Net无法识别的任何属性均按原样传递。

<asp:HyperLink ID="HyperLink1" runat="Server"
    NavigateUrl="https://plus.google.com/share?url={URL}"
    onclick="javascript:window.open(this.href, '',
      'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');
      return false;">
  ...
</asp:HyperLink>

In general, if you don't need server-side access to a control, I wouldn't recommend converting them to server-side because it adds extra unnecessary processing to the server. 通常,如果您不需要服务器端对控件的访问,我不建议您将它们转换为服务器端,因为这会给服务器增加不必要的处理。

To add it in the declarative syntax just add it ie 要以声明性语法添加它,只需添加它,即

<asp:HyperLink 
    runat="server" 
    ID="theLink" 
    onclick="javascript:window.open(this.href, '','menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');
  return false;">
</asp:HyperLink>

You must note that OnClick (which is event binding for ASP.NET and the lowercase version onclick are NOT the same) 您必须注意,OnClick(这是ASP.NET的事件绑定,而小写版本的onclick并不相同)

If you want to do it in code behind then you can do it via the WebControl.Attributes array ;) 如果要在后面的代码中执行此操作,则可以通过WebControl.Attributes数组;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM