Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call C# method in javascript function directly

Tags:

javascript

c#

How to call a c# method in javascript function directly. (eg page_load method of code behind page). Please help me.

like image 316
Bahareh Avatar asked Jun 23 '26 03:06

Bahareh


1 Answers

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')" .. /> 
like image 76
Akram Shahda Avatar answered Jun 24 '26 17:06

Akram Shahda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!