5

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

1
  • 1
    Could you give an example of what you mean? Commented May 7, 2011 at 21:42

2 Answers 2

6

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')" .. /> 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following way too:

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

1 Comment

this is such an ugly hack ;) where do you leave the button? Your other answer is much better.

Your Answer

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