skip to Main Content

I have button in aspx file as below :

<asp:Button ID="btnSave" Style="display: none;" runat="server" OnClick="btnSave_Click" />

Now in aspx.cs file I have below function.

protected void btnSave_Click(object sender, EventArgs e)
    {
//some code
    }

Now I want to call this btnSave_Click function from jquery, so I tried below but it is not calling this function..

function btnclick() {
$('#btnSave').click();
            }

Any idea, why function is not calling from this jquery code to cs file ?

Thanks

2

Answers


  1. You need to decorate a method in you aspx.cs file with WebMethod attribute like below:

    [WebMethod()]
    public static string GetData(string name)
    {
        //method body
    }
    

    Then you can make an AJAX call for it from Javascript.

    I think the below link will explain:
    https://www.c-sharpcorner.com/UploadFile/dacca2/static-webmethod-in-code-behind-webform/

    Login or Signup to reply.
  2. You can "click" a button, and I often do this is place of coding out a webmethod.

    So, add this to your button:

    <asp:Button ID="btnSave" 
              Style="display: none;" runat="server" 
              OnClick="btnSave_Click"
              ClientIdMode="static"
     />
    

    So, above then will allow your JavaScript code of this:

    function btnclick() {
        $('#btnSave').click();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search