C # ASP.NET call functions from JavaScript

I have a small working application for ASP.NET and C # and I would like to add Javascript to it.

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

What I would like to do is call some of the functions that I have in the code, depending on the response to the confirmation button.

How should I do it? Many thanks!

+3
source share
4 answers

Simple

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

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

In your javascript, in terms of verifying your code, simply do:

__doPostBack('TriggerMethod1', '');

And in your code, process this button to call Method1.

+3
source

ASHX , jQuery ajax .

. :

jQuery ASP.NET httphandlers

+1

, :

1- :

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

2- System.Web.UI.IPostBackEventHandler.RaisePostBackEvent, ( ).:

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

3- script, :

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

4 PostBack:

<a .... onclick="TriggerPostBack('control', 'arg')" .. /> 
+1

All Articles