views:

36

answers:

3

Forinstance there is a asp.net function. I want to run that function from javascript codes. is this possible ? if it is , how?

A: 

Yes, you have to addorn the "function" with [WebMethod] attribute and you could use JQuery to access it...

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Alexander
A: 

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.

Darin Dimitrov
Any reason for the downvote?
Darin Dimitrov
Yeah, I got the same result! Idiot down-voters, the scum of SO...
Alexander
+1 to offset...
Alexander
Thanks @Alex. I have nothing against downvotes, only they need to be justified by a comment explaining why the answer was wrong.
Darin Dimitrov
+2  A: 

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!

iamserious