如何在ajax中编码以删除记录或插入 [英] how to code in ajax to delete a record or insert

查看:75
本文介绍了如何在ajax中编码以删除记录或插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的例子。我想从那里获得一些想法。任何一个想法或简单的代码。

i希望通过ajax从数据库或其他例子中删除记录通过ajax添加记录我的代码在下面,但它不起作用。

我的后端方法

///

A simple example needed to me.I want to gain some idea from that.plz any one any idea or simple code.
i want to throug ajax i delete a record from data base or another example to add a record through ajax my code are below but it not work.
its my backend method
///

[WebMethod]
   public static void DeleteRec(string DeleteRecord1)
   {
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
       if (con.State == ConnectionState.Closed)
       {
           con.Open();
       }
       SqlCommand cmd = new SqlCommand("delete from tbl_Login where userID=2", con);
       cmd.ExecuteNonQuery();


       return;

   }



///

其前端代码

//


///
its front end code
//

<script>
        function DeleteRec(DeleteRecord1) {
            $.ajax({
                type: "POST",
                url: "frmNewRegisteration.aspx/DeleteRec",
                data: '{DeleteRecord1: ' + DeleteRecord1 + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("delete succesful");
                },
                error: function (response) {
                    alert("unable to delete");
                }
            });
        };

        </script>

推荐答案

.ajax({
type: POST
url: frmNewRegisteration.aspx / DeleteRec
data:' {DeleteRecord1:' + DeleteRecord1 + ' }'
contentType : application / json; charset = utf-8
dataType: json
成功:功能 (){
alert( delete success hasful);
},
错误: function (响应){
alert( 无法删除);
}
});
};

< / script>
.ajax({ type: "POST", url: "frmNewRegisteration.aspx/DeleteRec", data: '{DeleteRecord1: ' + DeleteRecord1 + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function () { alert("delete succesful"); }, error: function (response) { alert("unable to delete"); } }); }; </script>


检查DeleteRecord1中的值是什么似乎是空的



数据:'{DeleteRecord1:'+ DeleteRecord1 +'}'



另外我注意到在后端方法中你得到DeleteRecord1从前端和删除查询中,您将传递2作为参数。确保你可以使用DeleteRecord1(非null)。
Check what is the value in DeleteRecord1 is seems null in

data: '{DeleteRecord1: ' + DeleteRecord1 + '}'

Also i have noticed that in the backend method you are getting DeleteRecord1 from the front end and in the delete query you are passing 2 as a parameter. Make sure you can use DeleteRecord1 (not null).


你好,

使用UPdatePanel。

这是一个简单的例子

http://www.aspdotnet-suresh.com/2012/05/ajax-updapanel-control-with-triggers.html [ ^ ]


这篇关于如何在ajax中编码以删除记录或插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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