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....

2

1 Answer 1

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();
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.