Skip to content Skip to sidebar Skip to footer

How To Call C# Button Click Method From Javascript?

i am trying to call a server side button click method from javascript function but it's not working, can i know where i did wrong? aspx code:

EDITx2: Also, make sure your OnClientClick return value is true. If it's false, I think its equivalent to saying it's not valid and should not do a postback.


Solution 2:

ButtonFns.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(ButtonFns, "Your postback argument"));

GetPostBackEventReference allows you to get that piece of JavaScript, so that you can trigger that postback from elsewhere.

This article will help you to resolve this issue

How to call Postback from Javascript


Solution 3:

You can make a POST request from your client side firstly make your click event to method as:

[WebMethods]
public static void ClickEvent()
{
//do you stuff
}

and now make an ajax call from your client side let your web page name is Default.aspx:

$.ajax({
  type: "POST",
  url: "Default.aspx/ClickEvent",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

Solution 4:

In your case you can do it like that,

    <script type="text/javascript">

    function ImitatePressButton() {
            __doPostBack('ButtonFns', '');
    }
</script>

good luck.


Solution 5:

If you want to call a button from javascript, here is another way how.

 var test = document.getElementbyId("YourButtonName");

 test.onclick = function() {
        //your code

Post a Comment for "How To Call C# Button Click Method From Javascript?"