Call server side method from JSON

Posted by Zerotoinfinite on Stack Overflow See other posts from Stack Overflow or by Zerotoinfinite
Published on 2010-04-03T17:07:45Z Indexed on 2010/04/03 17:13 UTC
Read the original article Hit count: 342

Filed under:

How to call a method on a server side from JSON. Below is the code I am using

SERVER SIDE:

[WebMethod] private void GetCustomer( string NoOfRecords) {

    string connString = "Data Source=Something;Initial Catalog=AdventureWorks;Trusted_Connection=True;";
    SqlConnection sCon = new SqlConnection(connString);
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Sales.Customer WHERE CustomerID < '" + NoOfRecords+ "' ", sCon);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GvDetails.DataSource = ds.Tables[0];
    GvDetails.DataBind();

}

On Client Side:

var txtID = document.getElementById('txtValue'); $.ajax({ type: "POST", url: "Default.aspx/GetCustomer", data: "{Seconds:'" + txtID +"'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { alert(response); } });

Now I want that on button click, I would call the function at the client side [JSON], which pass the textbox value to the function. Please help

© Stack Overflow or respective owner

Related posts about JSON