Search code examples
c#methodshref

how to make href links fire C# methods when clicked


What I basically want to do is when I click on an href link in html it should fire a C# method attached to that link. What are my options on doing this? the html onclick only looks for javascript functions. also note I am not linking to another page, I just want to use it to add arguments to the URL and then run C# and use those arguments in a method.


Solution

  • You would have to create a C# webmethod or WebAPI which you would use ajax to call. You can not access a C# method directly from html or javascript unless you are using something like a server side control (LinkButton) which you could then add a C# handler for the click of that button.

    So client side your 'onClick' would execute a javascript function. The Javascript function would call the C# webmethod, passing in the parameters, then onSuccess you would have the results and do with them as you please.