使用javascript检查重复的数据 [英] check duplicate data with javascript

查看:176
本文介绍了使用javascript检查重复的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net写web应用程序。我有一个输入表单。我想要当客户点击保存按钮之前插入,检查这个数据是否在数据库中。我已经写了代码背后。但我想用java脚本做这个,因为当我使用页面刷新后的代码。这是我的.net代码,用于检查重复数据:

  SqlCommand commandrepeat1 = new SqlCommand(从CmDet选择代码,其中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)
{
MessageBox.Show(重复数据,再试一次!!!);
txtcode.Focus();
objconnection.Close();
return;
}
objconnection.Close();
}
catch
{
objconnection.Close();
}


解决方案

你应该有你的ASP。 NET按钮实现 OnClick 事件(一旦确定没有重复的数据,执行服务器端代码)和 OnClientClick 事件(执行您的JavaScript将调用以检查是否有重复的数据)。



我建议如下:



在JavaScript中,在您的按钮中添加一个jQuery点击事件,如下所示:

  $(#myButton ).click(function(){

});

注意:我已经将您的按钮的名称设为 myButton ,将其更改为与标记中的按钮的ID相匹配。



现在,您需要调用服务器端执行您的逻辑来查找重复数据。我建议使用通过jQuery .ajax()函数调用的ASP.NET AJAX页面方法,如下所示:

  $。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){
//这是一个重复的,带有消息
的警报用户//阻止服务器端点击发生return false;
return false;
}
}
});

最后,我们需要构建将处理由jQuery调用的页面方法的服务器端代码如下:

  [WebMethod] 
public static bool DoesDataExist()
{
SqlCommand commandrepeat1 = new SqlCommand(从CmDet选择代码,其中code =+ txtcode.Text +和company =+ DataBase.globalcompany.ToString()+按代码desc排序);
commandrepeat1.Connection = objconnection;
objconnection.Close();
objconnection.Open();
SqlDataReader drmax1;
drmax1 = commandrepeat1.ExecuteReader();
drmax1.Read();
if(drmax1.HasRows)
{
objconnection.Close();
返回true;
}
objconnection.Close();

返回false;
}


i am writing web application in asp.net . i have a input form . i want when the client click on save Button before insert, check this data is in data base or not . i have written it with code behind . but i want do this with java script because when i i use code behind the page refresh . this is my .net code for check duplicate data:

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)
            {
                MessageBox.Show("Duplicate data . try again!!! ");
                txtcode.Focus();
                objconnection.Close();
                return;
            }
            objconnection.Close();
        }
        catch
        {
            objconnection.Close();
        }

解决方案

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;
            return false;
        }
    }
});

Finally, we need to build the server-side code that will handle the page method called by the jQuery above, like this:

[WebMethod]
public static bool DoesDataExist()
{
    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();
        return true;
    }
    objconnection.Close();

    return false;
}

这篇关于使用javascript检查重复的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆