插入到保存数据中,加倍

时间:2019-06-24 09:22:11

标签: javascript c# sql-server

我正在尝试通过客户端的方法保存数据。但是当我每次检查数据库表时都保存了两倍。

我已经尝试了一切,但不能解决问题。

客户端

    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = strName;     
    cmd.Parameters.Add("@Country", SqlDbType.Int).Value = HotelCountry;
    cmd.Parameters.Add("@City", SqlDbType.Int).Value = HotelCity;
    cmd.Parameters.Add("@County", SqlDbType.Int).Value = HotelCounty;
    cmd.Parameters.Add("@Town", SqlDbType.Int).Value = HotelTown;
    cmd.Parameters.Add("@District", SqlDbType.Int).Value = HotelDistrict;
    cmd.Parameters.Add("@Km", SqlDbType.Int).Value = HotelKm;
    cmd.Parameters.Add("@Min", SqlDbType.Int).Value = HotelMin;
    cmd.Parameters.Add("@Direction", SqlDbType.Int).Value = HotelDirection;
     if (intId == 0)
    {
        cmd.CommandText = "INSERT INTO tabl_Hotels (fldName,fldCountry,fldCity,fldCounty,fldTown,fldDistrict,fldKm,fldMin,fldDirection) VALUES (@Name,@Country,@City,@County,@Town,@District,@Km,@Min,@Direction);";
    try
    {
    cmd.ExecuteNonQuery();
    Bootstrap.Success("Hotel saved successfully.");     
    }
    catch (Exception ex) { Bootstrap.Danger(ex.Message); }       
    Response.End();
    }

html按钮

 <button type="button" id="btn-save" class="btn btn-primary" data-webmethod="hotels.aspx/save" >Save</button>

java脚本

 $(document).on('click', '#btn-save', function(e) {
        var HotelName = document.getElementById('<%= HotelNameTextBox.ClientID %>').value,
            HotelCountry= document.getElementById('<%= HotelCountryDropDownList.ClientID %>').value,
            HotelCity=document.getElementById('<%= HotelCityDropDownList.ClientID %>').value,
            HotelCounty=document.getElementById('<%= HotelCountyDropDownList.ClientID %>').value,
            HotelTown=document.getElementById('<%= HotelTownDropDownList.ClientID %>').value,
            HotelDistrict=document.getElementById('<%= HotelDistrictDropDownList.ClientID %>').value,
            HotelDistancek=document.getElementById('<%= HotelDistancekTextBox.ClientID %>').value,
            HotelDistancem=document.getElementById('<%= HotelDistancehTextBox.ClientID %>').value,
            HotelDirection=document.getElementById('<%= HotelDirectionDropDownList.ClientID %>').value;

        if(HotelDistancem=="" || HotelDistancek=="" || HotelName=="" || HotelCountry==0 || HotelCity==0 || HotelCounty==0 || HotelTown==0 || HotelDistrict==0 || HotelDirection==0){ bootbox.alert('Do not leave any blank.!');return false;}
        alert('deneme');
        });

2 个答案:

答案 0 :(得分:0)

您可以尝试使用断点并调试代码。代码可能没有ispostback验证。

答案 1 :(得分:0)

问题已解决。我为Web方法提供了双重参考,其中一个是母版页,另一个是ON页。因此,当我使用保存方法时,它将运行两次。我从页面删除引用,问题解决了。

     <script src="client-side/js/webmethod.js"></script>
相关问题