1

I've got a EmployeeMaster page where i enter empcode,empname etc.After entering empcode,when i click on the empname textbox,i want to show a success image if no such empcode exists and a failure image if empcode exists..

Is there any way to show this using jquery ajax method ?

Heres how i've tried to call the textbox change function.

 $(document).ready(function(){
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    //Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
    prm.add_beginRequest(BeginRequestHandler);
    // Raised after an asynchronous postback is finished and control has been returned to the browser.
    prm.add_endRequest(EndRequestHandler);    
    AutoComp();//function for autofill textbox and it works perfectly.     
    $("#<%=txtEmpCode.ClientID %>").change(checkEmpCode);//calling textbox change  function

});
function checkEmpCode() {
    alert('hai');
}

Here alert is not displaying.How can i solve this issue....

ksg
  • 3,927
  • 7
  • 51
  • 97
  • 1
    This is an old question, but it should give you pointers: http://stackoverflow.com/questions/5756147/basic-simple-asp-net-jquery-json-example – Adriano Carneiro Sep 27 '12 at 15:37
  • I've used the WebMethod way before: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/. – Gromer Sep 27 '12 at 15:40

1 Answers1

0

Here is the javascript where you call the web service which check if the empcode is duplicate, the method will return 0 if duplicate

<script type="text/javascript"> 
  $(function() {
    $("#empcode").change(checkEmpCode);
  });

  function checkEmpCode() {
    $.ajax({
      type: "POST",
      url: "Service.asmx/CheckEmpCode",
      data: "{empcode: '" + $('#empcode').val() + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {            
        if (response.d != "0") {
          $("#failureimage").show();
        }
        else{
          $("#successimage").show();
        }

      }
    });
  }

</script> 

Your webservice will have this method

[WebMethod]
public int CheckEmpCode(string empcode)
{
  string connect = @"Server=SERVER;Database=Database;Trusted_Connection=True;";
  string query = "SELECT COUNT(*) FROM Employee WHERE empcode = @empcode";
  using(SqlConnection conn = new SqlConnection(connect))
  {
    using(SqlCommand cmd = new SqlCommand(query, conn))
    {
      cmd.Parameters.AddWithValue("empcode", empcode);
      conn.Open();
      return (int)cmd.ExecuteScalar();
    }
  }
}
Arjun Shetty
  • 1,575
  • 1
  • 15
  • 36
  • May be this should help http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/ – Arjun Shetty Sep 28 '12 at 04:28
  • sorry for the late response.I've written the change function,but the function is not working.I've tested the function with alert message.I've updated the above code – ksg Oct 23 '12 at 18:38
  • 1
    Yeah,finally i've got it.Thank you very much for ur wonderful answer.Without you i think i wont be able to achieve this... – ksg Oct 24 '12 at 18:46