[英]How to show success message using jquery ajax method in asp.net on textbox change?
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.. 我有一个EmployeeMaster页面,在其中输入empcode,empname等。输入empcode后,当我单击empname文本框时,如果不存在这样的empcode,我想显示成功图像,如果empcode存在,则要显示失败图像。
Is there any way to show this using jquery ajax method ? 有什么方法可以使用jquery ajax方法显示此内容吗?
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.... 这里没有显示警报。如何解决此问题...。
Here is the javascript where you call the web service which check if the empcode is duplicate, the method will return 0 if duplicate 这是您调用Web服务的javascript,用于检查empcode是否重复,如果重复,则该方法将返回0
<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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.