Check Duplicate Data With Javascript
Solution 1:
You should have your ASP.NET button implement both the OnClick
event (to execute server-side code once it is determined that there is not duplicate data) and OnClientClick
event (to execute your JavaScript that will call to check if there is duplicate data).
I suggest the following:
In JavaScript, add a jQuery click event to your button, like this:
$( "#myButton" ).click(function() {
});
Note: I have assumed the name of your button to be myButton
, change it to match the ID of your button in markup.
Now you will need to call server-side to execute your logic to look for duplicate data. I recommend using ASP.NET AJAX Page Methods invoked via the jQuery .ajax()
function, like this:
$.ajax({
type: "POST",
url: "YourPage.aspx/DoesDataExist",
data: "{'codeValue': $('#myTextBox').val()}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if(msg.d) {
// This is a duplicate, alert user with message// Block the server-side click from happening with return false;returnfalse;
}
}
});
Finally, we need to build the server-side code that will handle the page method called by the jQuery above, like this:
[WebMethod]
publicstaticboolDoesDataExist()
{
SqlCommand commandrepeat1 = new SqlCommand("Select code from CmDet where code = " + txtcode.Text + " and company = " + DataBase.globalcompany.ToString() + " order by code desc");
commandrepeat1.Connection = objconnection;
objconnection.Close();
objconnection.Open();
SqlDataReader drmax1;
drmax1 = commandrepeat1.ExecuteReader();
drmax1.Read();
if (drmax1.HasRows)
{
objconnection.Close();
returntrue;
}
objconnection.Close();
returnfalse;
}
Post a Comment for "Check Duplicate Data With Javascript"