2012-12-27 35 views
1

我正在使用Kendo网格来显示员工数据,并执行创建,更新和删除。读取操作表现不错,但来到其余三个操作没有反映回数据库CRUD操作不适用于数据库kendo ui web

这里是我试图

<div id="grdCRUD"> 
</div> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
dataSource = new kendo.data.DataSource({ 
      transport: { 
       read: { 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        url: "GridWebService.asmx/GetData" 
       }, 
       update: { 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: function (EmpNames) { 
         return "GridWebService.asmx/UpdateEmp" + EmpNames.eid 
        }, 
        dataType: "json" 
       }, 
       destroy: { 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        url: "GridWebService.asmx/DeleteEmp" 
       }, 
       create: { 
        url: "", 
        dataType: "jsonp" 
       }, 
       parameterMap: function (options, operation) { 
        if (operation !== "read" && options.models) { 
         return { models: kendo.stringify(options.models) }; 
        } 
       } 
      }, 
      batch: true, 
      pageSize: 6, 
      schema: { 
       data: "d", 
       model: { 
        id: "eid", 
        fields: { 
         ename: { validation: { required: true} }, 
         age: { type: "number", validation: { required: true, min: 1} }, 
         salary: { type: "number", validation: { required: true, min: 1} } 
        } 
       } 
      } 
     }); 
     $("#grdCRUD").kendoGrid({ 
      dataSource: dataSource, 
      pageable: { 
       refresh: true, 
       pageSizes: true 
      }, 
      height: 300, 
      toolbar: ["create"], 
      columns: [ 
            { field: "ename", title: "EmployeeName", width: "150px" }, 
            { field: "age", title: "EmployeeAge", width: "150px" }, 
            { field: "salary", title: "EmployeeSalary", width: "100px" }, 
            { command: ["edit", "destroy"], title: "&nbsp;", width: "210px" } 
           ], 
      editable: "popup" 
     }); 
}); 
</script> 

这里我的web服务代码

[WebMethod] 
public List<EmpNames> GetData() 
{ 
    SqlDataAdapter da = new SqlDataAdapter("select * from Emp", con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "emp"); 
    return LstEmpNames(ds); 
} 

public List<EmpNames> LstEmpNames(DataSet ds) 
{ 
    List<EmpNames> objenamelst = new List<EmpNames>(); 
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
    { 
     EmpNames objemp = new EmpNames(); 

     objemp.eid = Convert.ToInt32(ds.Tables[0].Rows[i][0]); 
     objemp.ename = ds.Tables[0].Rows[i][1].ToString(); 
     objemp.age = Convert.ToInt32(ds.Tables[0].Rows[i][2]); 
     objemp.salary = Convert.ToInt32(ds.Tables[0].Rows[i][3]); 
     objenamelst.Add(objemp); 
    } 
    return objenamelst; 
} 
[WebMethod] 
public DataSet DeleteEmp(int id) 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("delete Emp where eid=" + id, con); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    return null; 
} 

[WebMethod] 
public DataSet CreateEmp() 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Insert into Emp values", con); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    return null; 
} 

[WebMethod] 
public DataSet UpdateEmp(int eid) 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("update emp set ename='SHANKI',age=25,salary=6000 where eid=1", con); 
    con.Close(); 
    return null; 
} 

是什么,我已经错过了,或者如果代码是错误的CA ñ你给我任何示例代码非常感谢。

回答

2

这里是一个使用ASP.NET Web服务完全正常工作的CRUD应用程序:https://github.com/telerik/kendo-examples-asp-net/tree/master/grid-web-service-crud

与实现的问题是方法的签名 - 检查链接的例子方法应该是什么样子等。

约从JavaScript调用ASP.NET Web服务的一个详细的解释可以在这个优秀的博客文章中找到:http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

+0

感谢快速replay.I'm寻找html页面不是为ASP页面,因为我们正在开发该应用程序完全在HTML页面。基于Html可以为您提供任何示例代码,如您所提供的asp.net –

+0

您好Korchev我已采取.html页面和Web服务。我通过Web服务绑定从数据库到网格的值。但问题是我不能做更新和删除,插入操作。确保我没有使用Ajax绑定,只需使用正常的数据源。 –

+0

@parsanamoni对不起,这与原始问题有什么关系? –

相关问题