How to fire a server side event from javascript?

How can I trigger a server control event in client-side JavaScript?

+5
source share
2 answers

To call the client side method, you need to do the following:

1- Create a server side method:

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

2- Embed System.Web.UI.IPostBackEventHandler.RaisePostBackEventthat takes one string argument (you can name the value of this argument) .:

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

3- Write a script to invoke the response entry:

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

4 If necessary, call the PostBack launch function:

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

You can also use the following method:

<a id="myLink" href="#" 
    onclick="document.getElementById('<%=ServerControl.ClientID%>').Event(); 
    return false;">OK</a>
0
source

All Articles