Priyank Patel
Priyank Patel

Reputation: 7006

Calling Code Behind Method From Jquery

I am using Asp.net/C# in my project , i have a requirement where in the user enters a name and the details of that person is displayed asp.net gridview ,,,, i am planning to use html button instead of asp.net button because the results displayed will be in tab...However the function which will populate the gridview is in code behind ,, so my question is how will i call that method from jquery,,, is that possible.Or is there any better way of doing this...... Thanks in advance

Upvotes: 1

Views: 5188

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176956

Have look to this post which describe you how to call function of code behind : Calling Server Side function from Client Side Script

Cs File (codebehind)

[WebMethod] 
public static string IsExists(string value) 
{     
    //code to check uniqe value call to database to check this     
   return "True";
 } 

Javascript/jQuery

function IsExists(pagePath, dataString)
 {
  $.ajax({
     type:"POST",
     url: pagePath,
     data: dataString,
     contentType:"application/json; charset=utf-8",
     dataType:"json",
     error:
          function(XMLHttpRequest, textStatus, errorThrown) {               
               alert("Error");
          },
     success:
          function(result) {
                  alert( result.d);

      }
     }
});}

      var pagePath = window.location.pathname + "/IsExists";
     var dataString = "{ 'value':'ab" }";
     IsExists(pagePath, dataString);

Upvotes: 4

Related Questions