簡體   English   中英

如何使用jquery和c#將數據庫中的數據顯示到html表中

[英]how to display data from database into a html table using jquery and c#

如何使用 jquery 和 c# 將數據庫中的數據顯示到 html 表中。 ..幫助我在這里我只使用了一個我需要的html頁面以及webmethod(cs)和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>
$(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>

首先,我認為您錯誤地使用了回調參數Result.d ,在 webmethod 中,您返回沒有任何復雜字段的純字符串,因此請嘗試在您的回調中調用.html(Result)而不是.html(Result.d)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM