Mario
Mario

Reputation: 14741

How to add javascript to ASP.NET hyperlink control

How can I enter this URL into a HyperLink ASP.NET Control ?

<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>

Upvotes: 0

Views: 5676

Answers (2)

Paul Sullivan
Paul Sullivan

Reputation: 2875

To add it in the declarative syntax just add it i.e.

<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)

If you want to do it in code behind then you can do it via the WebControl.Attributes array ;)

Upvotes: 1

mellamokb
mellamokb

Reputation: 56769

Just use onclick. Any properties that are not recognized by ASP.Net are passed as-is.

<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.

Upvotes: 3

Related Questions