在我的 Table ASPX 页面中,第一行是空白,最后一行丢失

In My Table The First Row Is Blank And The Last Row Is Missing In ASPX Page

我尝试使用调试模式检查数据,它包含所有行,但是当我尝试显示时,第一行是空的,最后一行丢失了。

这是 table 的观点:

<table class="table table-bordered table-striped table-hover ">
<thead style="text-align-last: center; background-color: black; color: white">
    <th>Id</th>
    <th>Company</th>
    <th>City</th>
    <th>Opening Balance</th>
</thead>
<tbody id="myTable" style="text-align: center;">
    <%if (!object.Equals(ds.Tables[0], null))
                                            {
                                                if (ds.Tables[0].Rows.Count > 0)
                                                {%>
    <% for (int i = 0; i < ds.Tables[0].Rows.Count; i++)%>
    <%{ %>
    <tr>
        <td>
            <asp:label id="L1" runat="server" text=""></asp:label>
            <% L1.Text = ds.Tables[0].Rows[i]["com_id"].ToString(); %></td>
        <td>
            <asp:label id="L2" runat="server" text=""></asp:label>
            <% L2.Text = ds.Tables[0].Rows[i]["company_name"].ToString(); %></td>
        <td>
            <asp:label id="L3" runat="server" text=""></asp:label>
            <% L3.Text = ds.Tables[0].Rows[i]["openbal"].ToString(); %></td>
        <td>
            <asp:label id="L4" runat="server" text=""></asp:label>
            <% L4.Text = ds.Tables[0].Rows[i]["city"].ToString(); %></td>
    </tr>
    <% }
                                                }
                                            }%>
</tbody>

C# 代码:

public static Cmd(string q)
{
    con.Open();
    SqlDataAdapter command = new SqlDataAdapter(q, ConSetting.con);
    command.SelectCommand.CommandType = CommandType.StoredProcedure;
    con.Close();
    DataSet ds = new DataSet();
    command.Fill(ds);
}

你能用 Foreach 试试吗

<% foreach (DataRow dr in ds.Tables[0].Rows)%>
    <%{ %>
    <tr>
        <td>
            <asp:label id="L1" runat="server" text=""></asp:label>
            <% L1.Text = dr["com_id"].ToString(); %></td>
        <td>
            <asp:label id="L2" runat="server" text=""></asp:label>
            <% L2.Text = dr["company_name"].ToString(); %></td>
        <td>
            <asp:label id="L3" runat="server" text=""></asp:label>
            <% L3.Text = dr["openbal"].ToString(); %></td>
        <td>
            <asp:label id="L4" runat="server" text=""></asp:label>
            <% L4.Text = dr["city"].ToString(); %></td>
    </tr>
    <% }

我做了类似的事情,效果很好。但仍然困惑为什么上面的不起作用。

C#

 if (!object.Equals(ds.Tables[0], null))
 {
     if (ds.Tables[0].Rows.Count > 0)
     {
         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
         {
             htmlTable.Append("<tr>");
             htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["com_id"] + "</td>");
             htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["company_name"] + "</td>");
             htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["openbal"] + "</td>");
             htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["city"] + "</td>");
             htmlTable.Append("</tr>");
          }
             tabledata.Controls.Add(new Literal { Text = htmlTable.ToString() });
     }    
     else
     {
         htmlTable.Append("<tr>");
         htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
         htmlTable.Append("</tr>");
      }
  }

.aspx

<table class="table table-bordered table-striped table-hover ">
    <thead style="text-align-last: center; background-color: black; color: white">
        <th>Id</th>
        <th>Company</th>
        <th>Opening Balance</th>
        <th>City</th>
    </thead>
        <tbody id="myTable" style="text-align: center;">
            <asp:PlaceHolder ID="tabledata" runat="server"></asp:PlaceHolder>
        </tbody>
</table>