无法在加载时加载Gridview

时间:2017-10-14 10:25:50

标签: javascript c# jquery asp.net gridview

我无法在页面加载时加载gridview。它一直在给我

未捕获的TypeError:无法读取未定义的属性“length”。 我已经找到了下面的代码和处理程序。请帮我解决这个问题。问题出现在Jquery中。

 <script type="text/javascript">
             $(document).ready(function () {
                 BindGridView();

             });


 function BindGridView() {
             $.ajax({
                 type: "POST",
                 url: "../Pm/uc/G.ashx/GetMailDetail",
                 contentType: "application/json;charset=utf-8",
                 data: {},
                 dataType: "json",
                 success: function (data) {
                     if (data.d.length > 0) {
                         $("#grdDemo").append("<tr><th>Username</th></tr>");
                         for (var i = 0; i < data.d.length; i++) {

                             $("#grdDemo").append("<tr><td>" + 
                             data.d[i].Username + "</td> <td>");
                         }
                     }
                 },
                 error: function (result) {
                 }
             });
         }
        </script>

     <asp:GridView ID="grdDemo" runat="server">
        </asp:GridView>

这是在Handler中。(你可以用任何东西替换查询。)

 public void ProcessRequest(HttpContext context)
    {
        //int mailid = int.Parse(context.Request["mid"]);
        //var detail = GetMailDetail(mailid);
        var detail = GetMailDetail();

        if (detail != null)
        {
            context.Response.ContentType = "application/json";
            string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(detail);
            context.Response.Write(json);
        }
        else
        {
            context.Response.StatusCode = 404;
        }
    }

    //protected object GetMailDetail(int mailid)
    protected object GetMailDetail()
    {
        List<DetailsClass> Detail = new List<DetailsClass>();

        Connection Con = new Connection();
        String Connection = Con.Active_Connection();
        SqlConnection con = new SqlConnection(Connection);
        con.Open();
        DataTable dt = new DataTable();

        SqlCommand cmd = new SqlCommand("select Sp4_Txt from  Sp4", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dtGetData = new DataTable();

        da.Fill(dtGetData);

        foreach (DataRow dtRow in dtGetData.Rows)
        {
            DetailsClass DataObj = new DetailsClass();
            DataObj.Username = dtRow["Sp4_Txt"].ToString();
            Detail.Add(DataObj);
        }

        return Detail.ToArray();
    }
    public class DetailsClass //Class for binding data
    {
        public string Username { get; set; }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

无法读取属性&#39;长度&#39;未定义的。

此错误即将发生,因为您正在访问data.d而未检查data并访问data.d.length而未检查data.d

做这样的事情:

if(data){
    if(data.d){
        if (data.d.length > 0) {
                 $("#grdDemo").append("<tr><th>Username</th></tr>");
                 for (var i = 0; i < data.d.length; i++) {

                     $("#grdDemo").append("<tr><td>" + 
                     data.d[i].Username + "</td> <td>");
                 }
             }
      }
}