I Have a small working application on ASP.NET and C# and I would like to add some Javascript to it.

I know that for example I can use the Confirm button and then do something on yes or no.

What I would like to do is call some of the functions I have in the code-behind, depending on the answer to the Confirm Button.

How do I go about this? Many Thanks!

share|improve this question
Could you be more specific? Explain what you exactly want, what is are goals and requirements? – Caspar Kleijne May 12 '11 at 21:25
1  
See this question: stackoverflow.com/questions/3713/… – Aaron Daniels May 12 '11 at 21:26

4 Answers

up vote 2 down vote accepted

Simple.

Put a hidden div on your aspx page that contains button triggers to your methods:

<div style="display: none">
     <asp:Button ID="TriggerMethod1" runat="server" />
</div>

In your javascript, in the confirm part of your code simply do:

__doPostBack('TriggerMethod1', '');

And in your code-behind, handle up that button click to call Method1.

share|improve this answer
I personally use jQuery, and just trigger the click with: $("#TriggerMethod1").click();. – Scott May 12 '11 at 21:31
Thanks! This is what i was looking for. Clear and Concise! – NicoTek May 12 '11 at 22:30
No problem! Glad to help! – Scott May 12 '11 at 22:45

To call a server side method on a client side event you need to do the following:

1- Create the server side method:

void DoSomething(...) { ... }

2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent which take one string argument (You can assign the name to the value of this argument).:

public void RaisePostBackEvent(string eventArgument) 
{
        DoSomething(...);
}

3- Write a script to trigger post back:

function TriggerPostBack(control, arg){
    __doPostBack(control, arg);
}

4- Call the PostBack trigger function when needed:

<a .... onclick="TriggerPostBack('control', 'arg')" .. /> 
share|improve this answer

I would create an ASHX handler file and post back to the hander using jQuery ajax methods.

See an article on this here:

Using jQuery in ASP.NET apps with httphandlers

share|improve this answer

Have you tried to use the search first in the site!! Call ASP.NET Function From Javascript? Calling functions from an ASP.NET code file with javascript.

or http://stackoverflow.com/search?q=Call+C%23+Functions+From+JavaScript

share|improve this answer
Easy, my friend, easy! – YetAnotherUser May 12 '11 at 21:30
I'm just guiding him to search before he asks as we all do. And sorry if it was by a hard way – AMgdy May 12 '11 at 21:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.