Call C# method in javascript function directly

后端 未结 7 585
醉梦人生
醉梦人生 2021-01-21 02:10

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

7条回答
  •  情话喂你
    2021-01-21 02:46

    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:

     
    

提交回复
热议问题