0

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

1
  • 3
    You are looking for Ajax. en.wikipedia.org/wiki/Ajax Commented Aug 13, 2010 at 11:37

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks @Alex. I have nothing against downvotes, only they need to be justified by a comment explaining why the answer was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.