Q:

asp.net it's running my c# methods automatically

when i start my asp page, automatically my methods in code behind are executed and i don't want that.
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string ConvertDataTabletoString()
    {        
          //this method executes automatically
    }

I want the method ConvertDataTabletoString() run when I press a html button
ex. 
<!-- language-all: lang-html -->

Button OnClick="function()">Boton</Button

language: lang-js
<script>

    function function1()
    {
         cars = JSON.parse(ConvertDataTabletoString());
    }

</script>

A:

Add following HTML on page:
<asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
<asp:Button ID=”btnSave” runat=”server” Text=”Save” OnClientClick=”return CodeBehindMethodCall();” />

Now time to adjust our code behind so we can call it from JavaScript, we need to use System.Web.Services so add it in our code behind file
using System.Web.Services;

Whatever method we need to call from JavaScript, add WebMethodattribute to that method and that will easily be called by javaScript
[WebMethod]
public String ConvertDataTabletoString()
{
    // your code 
}

Now we will call ConvertDataTabletoString from JavaScript, so add the following JavaScript to the page:
function CodeBehindMethodCall()
{
    pageName.ConvertDataTabletoString();
}

As you can see we have not used web service but we changed a method to web method so it can be called from JavaScript but without converting a method into web method we can not call any code behind method from JavaScript.
This is how its done.

