Amresh Amresh - 2 months ago 14
Vb.net Question

Click an anchor tag in vb.net

Please suggest a method in vb.net to perform 'onclick' event on clicking the image in anchor tag.

<td style="border: 0px solid white; border-image: none;">

<div id="clickAnImage100">

<a onclick="GetMyWebsite('http://www.google.com');" href="#!">
<img height="34" src="../images/WebClick.png">
</a>

</div> </td>


Thanks!!

Answer

In any of your JavaScript file, you can simply do that by

$( "img" ).click(function() {
  //your logic
});

--OR--

give a class to your image say trigger

<a onclick="GetMyWebsite('http://www.google.com');" href="#!">
   <img height="34" src="../images/WebClick.png" class="trigger">
</a>

& then in your JavaScript file you can

$( ".trigger" ).click(function() {
  //your logic
});

If you're looking to "programmatically" call a click inside the onclick event, then what you need is trigger() method of jquery. This function "fakes" a click event on an element. Can be used like:

function GetMyWebsite(link_passed) {
  $(".first").attr("href", link_passed);
  $( ".second" ).trigger( "click" );
  //your logic
}

$( ".second" ).click(function() {
  alert('You called me from first');
});

Here is a codepen I created to show you the same. Also the explanation to tigger() method from JQueryAPI.

Updated answer as you need VB.net way of getting this done.

In VB.net, all you need is PerformClick() method like so:

Obviously you need to replace the Button2 to your desired Control(anchor tag control) which you want to click

Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    'do stuff
    Me.Button2.PerformClick()
End Sub

This is just an example of PerformClick(), you can call it wherever you want to & make perform a click on the anchor tag