Forinstance there is a asp.net function. I want to run that function from javascript codes. is this possible ? if it is , how?
2 Answers
This is not the best way to do it, its a quick fix, cunning, but it works!
just add a asp button somewhere in the page and in the button click function, call the method you want to call from javascript.
suppose you have a c# function in your code behind like
public void doSomething() {...}
then the button function will something be like:
protected void button1_onClick(object sender, eventArgs e)
{
doSomething();
}
and your aspx page will have a button like this, make sure you add style property to hide the button (if you want to hide it)
<asp:button ID="button1" runat="server" text="" style="display:none"
class="NinjaButton" onClick="button1_onClick" />
now, when you want to call the function, just click the invisible button through javascript (or even better, jquery)
suppose you want to call the function when the mouse moves over a div, you can do this:
<div id="divSomething" OnMouseOver="$('.NinjaButton').click()">
....
</div>
Remember, the NinjaButton in the jquery on mouseover is just the class name of that button. make sure you dont have that class to any other button!
Comments
There's no such thing as asp.net function. If you mean a .NET method which is defined in the code behind of an ASP.NET page you could take a look at PageMethods. And here's an example using jQuery. Scott Gu has also blogged about them. Note that the method needs to be static.