Raise Server side button click event from javascript in ajax call

前端 未结 2 2042
既然无缘
既然无缘 2020-12-10 17:40

I have a submit button on a page.



        
2条回答
  •  执念已碎
    2020-12-10 18:30

    You can get the required JavaScript code from the ClientScriptManager's GetPostBackEventReference method:

    Returns a string that can be used in a client event to cause postback to the server.

    This is normally used for writing the onclick attributes on controls like the , but you can use it in your jQuery callback as well:

    var succeededAjaxFn = function(result) {
      //Raise server side button click event. Dont call click side event anymore.
      <%= Page.ClientScript.GetPostBackEventReference(btnSubmit, String.Empty) %>;
    }
    

    The <%= %> block above will write out the following JavaScript for you:

    __doPostBack('btnSubmit','')
    

    Which in turn will post back the form to the server in such a way that ASP.NET thinks the button was clicked, and so the server-side btnSubmit_Click is triggered.

    Notice that using this method, you can pass in a C# reference to the actual control. You don't need to worry about its client ID, or the correct name and arguments of the __doPostback() JavaScript function. All that is taken care of by the ClientScriptManager when you call this method.

提交回复
热议问题