使用ASP.NET的Jquery - 我的页面方法不返回所有记录

时间:2011-01-31 06:41:36

标签: jquery

我无法使用页面方法来使用asp.net jquery的东西来获得我想要的东西。 我有一个简单的表我从中获取数据,但我只从我的aspx页面中的页面方法返回一行。如果有人可以提供帮助,下面的代码 在此先感谢

[WebMethod]  
 public static SComms comms()  
 {  
     SComms c = new SComms();  
     string connect = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
     string query = "select * from dbo.Comms where dateadd(dd, datediff(dd, 0, created), 0) = dateadd(dd, datediff(dd, +10, getdate()), 0) order by 2";  

         using (SqlConnection conn = new SqlConnection(connect))  
         {  
             using (SqlCommand cmd = new SqlCommand(query, conn))  
             {  

                 conn.Open();  
                 SqlDataReader rdr = cmd.ExecuteReader();  
                 if (rdr.HasRows)  
                 {  
                     while (rdr.Read())  
                     {  
                         c.ListID = rdr["ListID"].ToString();  
                         c.ListID = rdr["Title"].ToString();  
                     }  
                 }  
             }  
         }  
     //}  
     return c;  
 }  

Javascript

<script type="text/javascript">
    $(document).ready(function() {        
        $.ajax({  
             type: "POST",  
             contentType: "application/json; charset=utf-8",  
             data: "{}",  
             url:"page.aspx/Comms",  
             dataType: "json",  
             success: function(data) {  
                 if (data.hasOwnProperty("d"))  
                    DoSomething(data.d);  
                 else  
                    DoSomething(data);  

             }  
    });  

    function DoSomething(msg) {  
        //$("quote_wrap").append(msg);  
        var SComms = msg;  
        $('quote_wrap').append  

        //I can only get one record here  
        alert(SComms.Title);  
    }         
  });  

</script>  

我想要的输出是:

 <blockquote>  
     <p>Ut eu consectetur nisi. Praesent facilisis diam nec sapien gravida non mattis justo imperdiet. Vestibulum nisl urna, euismod sit amet congue at, bibendum non risus.</p>  
     <cite>– Quote Author (Quote 1)</cite>  
 </blockquote>  

1 个答案:

答案 0 :(得分:0)

我不知道你的SComms对象的内部结构,但看起来你没有做任何事情来创建多个记录。你只是一遍又一遍地设置一个对象的ListID属性。

如果SComms是普通类,并且您想要返回它们的集合,则可能会执行以下操作:

[WebMethod]  
public static SComms comms()  
{  
  var result = new List<SComms>();   

  string connect = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  string query = "select * from dbo.Comms where dateadd(dd, datediff(dd, 0, created), 0) = dateadd(dd, datediff(dd, +10, getdate()), 0) order by 2";  

  using (SqlConnection conn = new SqlConnection(connect))  
  {  
    using (SqlCommand cmd = new SqlCommand(query, conn))  
    {            
      conn.Open();  
      SqlDataReader rdr = cmd.ExecuteReader();  

      // No need to test for HasRows first. If there aren't any,
      //  the while won't execute and the method will return
      //  an empty array which is easy to test for on the client-side.
      while (rdr.Read())  
      {  
        var c = new SComms();

        c.ListID = rdr["ListID"].ToString();
        c.Title = rdr["Title"].ToString();

        result.Add(c);
      }  
    }  
  }

  return result;  
}