Search code examples
c#jquery.netasp.net-mvcwebforms

Call aspx.cs file function from jquery


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


Solution

  • 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();
    }