C# 如何使用jquery和c将数据库中的数据显示到html表中

C# 如何使用jquery和c将数据库中的数据显示到html表中,c#,jquery,html,C#,Jquery,Html,如何使用jquery和c将数据库中的数据显示到html表中。 …帮帮我 在这里,我只使用了一个我需要的html页面以及webmethodcs和jquery。 当我单击该按钮时,它应该显示在html表中,其中包含存储的数据库值,仅在客户端的html页面中使用jquery和cs, 这是我的应用程序代码 wservice.cs ____________ public class LeaveApplicationDisplay {

如何使用jquery和c将数据库中的数据显示到html表中。 …帮帮我 在这里,我只使用了一个我需要的html页面以及webmethodcs和jquery。 当我单击该按钮时,它应该显示在html表中,其中包含存储的数据库值,仅在客户端的html页面中使用jquery和cs, 这是我的应用程序代码

    wservice.cs
     ____________
      public class LeaveApplicationDisplay
             {
              public string Branch { get; set; }
              public string Id { get; set; }
              public string EmpName { get; set; }
               public string Reason { get; set; }            
           }

     public List<LeaveApplicationDisplay> GetLeaveApplicationDisplayInfo { get; set; }
         [WebMethod]
         public string GetLeaveApplicationDisplay(string id)
         {
             string strhtml = string.Empty;
            try
              {
                 strhtml = GetLeaveApplicationDisplay();
             }
             catch (Exception ex)
           {

            }
            return strhtml;
                }                
   public static string GetLeaveApplicationDisplay() {
   LeaveApplicationDisplay lad = new LeaveApplicationDisplay();
   DataTable table = new DataTable();
  using (SqlConnection con = new SqlConnection(@"server=.;uid=sa;password=password;database=SMS_WORK;")) {
 using (SqlDataAdapter sda = new SqlDataAdapter("SELECT U_Branch,U_empID,U_empName,U_Reason FROM SMS_LEAVE", con)) {
     con.Open();
        sda.Fill(table);
      }}
       DataSet ds = new DataSet();
         ds.Tables.Add(table);                  
      System.Text.StringBuilder HTML = new System.Text.StringBuilder();
       HTML.AppendLine("<table>");

       foreach(DataRow dr in ds.Tables[0].Rows){
        HTML.AppendLine("<tr>");
         HTML.AppendFormat("<td>{0}</td>\n", dr["U_Branch"]);
         HTML.AppendFormat("<td>{0}</td>\n", dr["U_empID"]);
         HTML.AppendFormat("<td>{0}</td>\n", dr["U_empName"]);
        HTML.AppendFormat("<td>{0}</td>\n", dr["U_Reason"]);
       HTML.AppendLine("</tr>");
         }
        HTML.AppendLine("</table>");
       return HTML.ToString();                               
     }

     leave.html
      __________

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

     function LeaveApplicationDetails() {        
        $("#Button1").click(function () {          
      $.ajax({                     
      url: "wservice.asmx/GetLeaveApplicationDisplay",
     contentType: "application/json; charset=utf-8",
       data: "{}",
       dataType: "json",
     type: "POST",
       success: function (Result)
     { $("#displayLeaveInformation").html(Result.d); }
      });
      return false;
     });
      }
     </script>
      <body>
         ..
      <div id="displayLeaveInformation">
          ..
     </body>

首先,我认为您错误地使用了回调参数Result.d,在webmethod中,您返回的是纯字符串,没有任何复杂字段,因此请尝试在回调中调用.htmlResult而不是.htmlResult.d.

您能从F12开发工具检查数据服务器发送给您的内容吗?是的,按F12,转到网络选项卡,选择要监视的请求,然后转到响应选项卡。这是一个你需要的例子-
$(document).ready(function () {

      $.ajax({

          url: "WebService2.asmx/GetLeaveApplicationDisplay",
          contentType: "application/json; charset=utf-8",
          data: "{ 'pMenuID': '" + getParameterByName('MenuID') + "'}",
          dataType: "json",
          type: "POST",
          success: function (result) {
              result = result.d;
              var ta = document.getElementById('dataTable');
              ta.innerHTML = result;

          }
      });
  });

<body>

  <div id="dataTable">

 </body>