如何从客户端加载asp gridview控件?

时间:2014-01-01 06:36:42

标签: javascript jquery asp.net ajax gridview

我在Web服务中有一个方法,如下所示:

[WebMethod(EnableSession = true)]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public location[] getLocation()
 {
     string companyCode = Convert.ToString(Session["companyCode"]);
     //string userID = Convert.ToString(Session["userID"]);
     DataTable dt = new DataTable();
     //DataSet ds = new DataSet();
     List<location> list = new List<location>();

     try
     {
         conn.Open();

         SqlCommand cmd = new SqlCommand();
         cmd.Connection = conn;
         cmd.CommandText = "getLocation";
         cmd.Parameters.Clear();
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@companyCode", companyCode);
         cmd.Parameters.AddWithValue("@status", DBClass.cns_Active);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
         conn.Close();
         foreach (DataRow dr in dt.Rows)
         {
             var loc = new location();
             loc.ID=Convert.ToInt32(dr["ID"]);
             loc.APIAddress = Convert.ToString(dr["APIAddress"]);
             loc.PostCode = Convert.ToString(dr["postCode"]);
             loc.ShostCode = Convert.ToString(dr["shortCode"]);
             loc.latLong=Convert.ToString(dr["latLong"]);
             list.Add(loc);
         }
     }
     catch
     {
         conn.Close();
     }
     return list.ToArray();
 }   

其中location是一个类

我试图从客户端使用此方法加载网格,这里是代码:

function getItems() {
$.ajax({
    type: "POST",
    url: "../WebService.asmx/getLocation",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (data) {
        for (var i = 0; i < data.d.length; i++) {
            $('#gvTrip').append("<tr class='gradeX'><td>" + data.d[i].ID + "</td><td>" + data.d[i].APIAddress + "</td><td>" + data.d[i].PostCode + "</td><td>" + data.d[i].ShostCode + "</td><td>" + data.d[i].latLong + "</td></tr>");
        }
    },
    error: function () {
        alert("Error in fetching record");
    }


});
}

此处gvTrip是一个asp gridview控件

所以我面临的问题是网格没有加载数据但是数据来自那个方法,我已经使用alert检查了 请帮忙

1 个答案:

答案 0 :(得分:0)

ajax中的gvTrip是服务器端控件。所以尝试使用:

$('<%=gvTrip.ClientID %>').append("Your string");