0
我想驗證產品代碼是否使用ajax調用(在jquery中)用於寫入web方法的webservice。現在,如果成功功能作爲「重複產品代碼」執行,則不應允許用戶保存記錄。所以我該如何檢查這個保存按鈕單擊事件在asp.net中爲webservice製作ajax調用(在jquery中)
我想驗證產品代碼是否使用ajax調用(在jquery中)用於寫入web方法的webservice。現在,如果成功功能作爲「重複產品代碼」執行,則不應允許用戶保存記錄。所以我該如何檢查這個保存按鈕單擊事件在asp.net中爲webservice製作ajax調用(在jquery中)
首先,在後面的頁面代碼中創建下面的方法。
using System.Web.Services;
[WebMethod]
public static bool CheckDuplicateCode(string productCode)
{
bool isDuplicate = false;
int pCode = Convert.ToInt32(productCode);
//check pCode with database
List<int> productCodes = GetProductCodeInDb();
foreach (var code in productCodes)
{
if (pCode == code)
{
isDuplicate = true;
break;
}
}
return isDuplicate;
}
,並在頁面標記結束body標籤插入
<script type="text/javascript">
$(document).ready(function() {
$('#<%=btnSave.ClientID %>').click(function() {
SaveProduct();
});
});
function SaveProduct() {
//Get all the data that you are trying to save
var pCode = $('#<%= txtProductCode.ClientID %>').val();
//pass the product code to web method to check for any duplicate
$.ajax({
type: "POST",
url: "/InsertProductPage.aspx/CheckDuplicateCode",
data: "{'productCode': '" + pCode + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
AjaxSuccees(msg);
},
error: AjaxFailed
});
}
function AjaxSuccees(msg) {
if (msg.d == true) {
return true;
//insert the rest of data
}
else {
alert("Product code already exists");
return false;
}
}
function AjaxFailed(msg) {
alert(result.status + ' ' + result.statusText);
}
</script>
希望之前這段代碼這有助於