我无法使用jquery ajax和web方法删除kendo网格中的记录 [英] am not able to delete record in kendo grid using jquery ajax and web method

查看:150
本文介绍了我无法使用jquery ajax和web方法删除kendo网格中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的.CS代码

THIS IS MY .CS CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace method
{
    public partial class method : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [System.Web.Services.WebMethod]

        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static string GetData()
        {
            // int cid = Convert.ToInt32(id);
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("select * from data1", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    List<dictionary><string,>> rows = new List<dictionary><string,>>();
                    Dictionary<string,> row;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Dictionary<string,>();
                        foreach (DataColumn col in dt.Columns)
                        {
                            row.Add(col.ColumnName, dr[col]);
                        }
                        rows.Add(row);
                    }
                    return serializer.Serialize(rows);
                    //return s;
                }
            }
        }

        //[System.Web.Services.WebMethod]
        //// [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        //public static string InsertStudents(string id, string fname, string lname, string mobileno)
        //{
        //    int i = Convert.ToInt32(id);
        //    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
        //    con.Open();
        //    SqlCommand cmd = new SqlCommand("up", con);
        //    cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(id);
        //    cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = fname;
        //    cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname;
        //    cmd.Parameters.Add("@mobileno", SqlDbType.NVarChar).Value = mobileno;
        //    cmd.CommandType = CommandType.StoredProcedure;

        //    cmd.ExecuteNonQuery();
        //    con.Close();


        //    return "record inserted successfully.";
        //}
        [WebMethod]
        public static string UpdateStudents(string id, string fname, string lname, string mobileno)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
            con.Open();

            SqlCommand cmd = new SqlCommand("UpdateS", con);
            cmd.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32(id);
            cmd.Parameters.Add("@fname",SqlDbType.VarChar).Value=fname;
            cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname;
            cmd.Parameters.Add("@mobileno", SqlDbType.NVarChar).Value = mobileno;
            cmd.CommandType = CommandType.StoredProcedure;
            // cmd.Parameters.AddWithValue("@fname", details.FirstName);
            // cmd.Parameters.AddWithValue("@lname", details.LastName);
            // cmd.Parameters.AddWithValue("@mobileno", details.MobileNo);
            // cmd.Parameters.AddWithValue("@id", details.RollNo);
            // con.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = cmd;
            da.Fill(ds);
           
          
            return "record updated successfully";
        }
        [WebMethod]
        public static string DeleteStudents(string id)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
            SqlCommand cmd1 = new SqlCommand("delete from data1 where id=@id ", con);
            cmd1.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32(id);
            cmd1.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd1.ExecuteNonQuery();
            con.Close();
            return "record delete successfully";
        }

    }

}







这是我的ASPX代码




THIS IS MY ASPX CODE

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="method.aspx.cs" Inherits="method.method" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/kendo/kendo.common.min.css" rel="stylesheet" type="text/css" />
    <link href="Styles/kendo/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
    <link href="Styles/kendo/kendo.default.min.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/kendo/jquery.min.js" type="text/javascript"></script>
    <script src="Scripts/kendo/kendo.all.min.js" type="text/javascript"></script>
</head>
<body>
           <div id="example" class="k-content">


           <div id="grid">
           </div>

  <%--<script type="text/x-kendo-template" id="gridTemplate">
       <table id="table" >
           <tr>
               <th >Roll No</th>

               <td><input type="text" id="txtRollNo" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th >First Name</th>
               <td><input type="text" id="txtFirsNamet" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th > Last Name  </th>
               <td><input type="text" id="txtLastName" class="k-input k-textbox" /></td>
           </tr>
           <tr>
               <th >Mobile No</th>
               <td><input type="text" id="txtMobileNo" class="k-input k-textbox" />
                    </td>

           </tr>

       </table>


     </script>
       --%>

           <script>

               $(document).ready(function () {
                 
                   var url = "method.aspx/GetData";
                   var url1 = 'method.aspx/UpdateStudents';
                   var url2 = 'method.aspx/DeleteStudents';
                   var url3="method.aspx/InsertStudents";

                   dataSource = new kendo.data.DataSource({
                       
                       transport: {
                           read: function (options) {
                               $.ajax({
                                   type: "GET",
                                   url: url,
                                   contentType: "application/json; charset=utf-8",
                                   //data:{'id':"66"},
                                   dataType: "json",

                                   success: function (result) {
                                       if (result.d === "error") {
                                           alert('Error occured. Please try again');
                                       }

                                       options.success(JSON.parse(result.d));

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                           },


                           update: function (options) {

                               var gview = $("#grid").data("kendoGrid");

                               var selectedItem = gview.dataItem(gview.select());


                               $.ajax({
                                   type: "POST",
                                   url: url1,
                                   
                                   contentType: "application/json; charset=utf-8",




                                   
                                   data: JSON.stringify({ 'id': selectedItem.id, 'fname': selectedItem.fname, 'lname': selectedItem.lname, 'mobileno': selectedItem.mobileno }),
                                  
                                  
                                   

                                   dataType: "json",
                                   success: function (result) {
                                       if (result.d !== "success") {
                                           
                                       }
                                       dataSource.read();

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                              


                           },
                           destroy: function (options) {

                               var gview = $("#grid").data("kendoGrid");

                               var selectedItem = gview.selectedItem(gview.select());
                              
                               gview.dataSource.remove(selectedItem);
                               gview.dataSource.remove(0);
                              



                               $.ajax({
                                   type: "POST",
                                   url: url2,
                                   contentType: "application/json; charset=utf-8",

                                   
                                   data: JSON.stringify({ 'id': selectedItem.id }),
                                   dataType: "json",
                                   success: function (result) {
                                       if (result.d !== "success") {
                                           alert(result.d);
                                       }
                                       dataSource.read();

                                   },
                                   error: function (result) {
                                       alert(result.Id);

                                   }
                               });
                           },




                           parameterMap: function (options, operation) {
                               if (operation !== "read" && options.models) {
                                   return { models: kendo.stringify(options.models) };
                               }
                           }
                       },
                       batch: true,
                       pageSize: 20,
                       schema: {
                           model: {
                               id: "id",
                               fields: {
                                   id: { from: "id", type: "number" },
                                   fname: { from: "fname", type: "string" },
                                   lname: { from: "lname", type: "string" },
                                   mobileno: { from: "mobileno", type: "string" }

                               }
                           }
                       }
                   });

                   $("#grid").kendoGrid({

                       dataSource: dataSource,
                       pageable: true,

                       selectable: "row",
                       mobile: "phone",
                       scrollable: true,

                       height: 430,
                      {mode:'inline',template:serviceEditTemplate}"
                      
                       toolbar: ["create", "save", "cancel"],
                       columns: [
                           "id",

                           { field: "fname", title: "First Name", width: "100px" },
                        { field: "lname", title: "Last Name", width: "100px" },
                        { field: "mobileno", title: "MobileNO", width: "100px" },
                           { command: ["edit", "destroy"], title: "&nbsp;", width: "172px"}],

                      
                       editable: {
                           
                           mode: "inline"
                           
                       },

                   });
               });

           </script>
       </div>


</body>

</html>

推荐答案

(文件).ready(function() {

var url = method.aspx / GetData;
var url1 = ' method.aspx / UpdateStudents';
var url2 = ' method.aspx / DeleteStudents';
var url3 = method.aspx / InsertStudents;

dataSource = new kendo.data.DataSource({

transport:{
读取:function(options){
(document).ready(function () { var url = "method.aspx/GetData"; var url1 = 'method.aspx/UpdateStudents'; var url2 = 'method.aspx/DeleteStudents'; var url3="method.aspx/InsertStudents"; dataSource = new kendo.data.DataSource({ transport: { read: function (options) {


.ajax({
type: GET
url:url,
contentType: application / JSON; charset = utf-8
// data:{'id':66 },
dataType: json

成功:函数(结果){
如果(result.d === 错误){
alert(' Error occured. Please try again');
}

options.success(JSON.parse(result.d));

},
error: function (result) {
alert(result.Id);
$b$ b }
});
},


update: function (options) {

var gview =
.ajax({ type: "GET", url: url, contentType: "application/json; charset=utf-8", //data:{'id':"66"}, dataType: "json", success: function (result) { if (result.d === "error") { alert('Error occured. Please try again'); } options.success(JSON.parse(result.d)); }, error: function (result) { alert(result.Id); } }); }, update: function (options) { var gview =


(\"#grid\").data(\"kendoGrid\");

var selectedItem = gview.dataItem(gview.select());
("#grid").data("kendoGrid"); var selectedItem = gview.dataItem(gview.select());


这篇关于我无法使用jquery ajax和web方法删除kendo网格中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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