samedi 25 avril 2015

invoking method using ajax at code behind in asp.net


hey i am making a simple web form its a product detail insertion web page. I am trying to insert using ajax call. without ajax it works .. but $.ajax is not invoking my code behind static method, no idea wat's the issue. here's the code:

$(document).ready(function () {
    $("#submit").click(function () {
        var cat = document.getElementById('DropDownList1').value;
        var nm = document.getElementById('name').value;
        var cde = document.getElementById('code').value;
        var dt = document.getElementById('dt').value;

        var price = document.getElementById('price').value;
        var f3 = document.getElementById('ty').innerHTML;

        alert("you clicked " + cat + " - " + nm + "-" + cde + "-" + dt + 
                "-" + price + "-" + f3 +  "-");

       //////////////uptil here alert gives the right value.

       $.ajax({
           method: "POST",
           contentType: "application/json", 
           url: "home.aspx/ins",
           dataType: "json",
           data: "{'Name :' + nm + 'code :' + cde +'category :'+ cat + 
              'date :'+ dt +'price :'+ pr +'img_name :' + f3}",
           //data:"{}",
           //async: false,
           success: function (response) {
               alert("User has been added successfully.");
               window.location.reload();
           }
       });
    })
});


//////////////////////////////// here is the code behind method:

[System.Web.Services.WebMethod]
public static void ins(string Name,string code,string category, DateTime date,
   int price,string img_name)
{
    productclass pc = new productclass();
    pc.Pr_name = Name;
    pc.Code = code;
    pc.Category = category;
    pc.Expiry = date;
    pc.Price = price;
    pc.Pr_image = img_name;

    dalinsert di = new dalinsert();
    bool flag = di.insert(pc);
}


Aucun commentaire:

Enregistrer un commentaire