【问题标题】:C# Make HTML Table repeating rowsC# 制作 HTML 表格重复行
【发布时间】:2017-08-08 12:39:36
【问题描述】:

我有这个 HTML 表格:

<table ID="tbl_ClientSearch" style="width: 83%;" runat="server">
   <thead>
      <tr>
         <td>Client Account Number</td>
         <td>Client Name</td>
      </tr>
   </thead>
   <tbody>
      <tr>                  
         <td><asp:Literal ID="CliNox" runat="server"></asp:Literal></td>      
         <td><asp:Literal ID="CliNamex" runat="server"></asp:Literal></td>            
      </tr>
   </tbody>
</table>

然后我有这个查询:

SqlCommand command = new SqlCommand(@"EXEC [dbo].[CTLC_SearchClient] '" + SearchVal + "';");
SqlConnection conn = new SqlConnection(connString);
conn.Open();
command.Connection = conn;
SqlDataReader readers = command.ExecuteReader();
if (readers.HasRows)
{
   while (readers.Read())
   {
      string CliNo = readers.GetString(0);
      string CliName = readers.GetString(1);
      CliNox.Text = string.Format("<a href='ClientInfo.aspx?ClientNumber={0}'>{0}</a>", CliNo.ToString());
      CliNamex.Text = CliName.ToString();
   }
   conn.Close();
}

这可行,但是当我需要返回多行时,这只返回 1 行,因为我只为表体声明了 1。我该如何解决这个问题?提前谢谢你。

【问题讨论】:

  • 您是否检查过您的 SP 是否返回多行?
  • @MAdeelKhalid andeel Khalid 是的。它工作正常。但是我的程序只显示最后一行,因为我认为它会覆盖 html 表格行上的值。
  • 即写你的程序应该只显示最后一行,你已经这样编码,即你只有一个 并且你正在覆盖该行单元格,你需要动态创建表结构或简单地使用 Gridview 或中继器控件。
  • 使用中继器控件:msdn.microsoft.com/en-us/library/zzx23804(v=vs.85).aspx。此外,您的 SQL 命令对 SQL 注入攻击是开放的。
  • 您可以使用 GridView、Repeater 或 asp:table 以表格格式呈现多行。参考这里,stackoverflow.com/a/139308/405317

标签: c# html asp.net html-table sqldatareader


【解决方案1】:

我不熟悉asp.net,但是这种情况看起来与php中可能发生的一些情况相似,在这种情况下,我会尝试在asp中生成整个表。类似的东西,

for row in tables
    append to html <tr><td>data</td><td>moredata</td></tr>

insert html into <table></table>

【讨论】:

    【解决方案2】:

    在 Aspx 页面中,在表格之外添加占位符,如下所示。

    <asp:PlaceHolder ID = "tableClientSearch" runat="server"/>
    

    在 Aspx.cs 文件中。

     StringBuilder html = new StringBuilder();
                    html.Append("<table border = '1'><thead><tr><td>Client Account 
    
    Number</td><td>Client Name</td></tr></thead>");
    
            SqlCommand command = new SqlCommand(@"EXEC [dbo].[CTLC_SearchClient] '" + SearchVal + "';");
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            command.Connection = conn;
            SqlDataReader readers = command.ExecuteReader();
    
            if (readers.HasRows)
            {
                while (readers.Read())
                {
                    html.Append("<tr>");
                    html.Append("<td>");
                    html.AppendFormat("<a href='ClientInfo.aspx?ClientNumber={0}'>{0}</a>", readers.GetString(0));
                    html.Append("</td>");
                    html.Append("<td>");
                    html.Append(readers.GetString(1));
                    html.Append("</td>");
                    html.Append("</tr>");
                }
                conn.Close();
            }
    
            html.Append("</table>");
            tableClientSearch.Controls.Add(new Literal { Text = html.ToString() });
    

    【讨论】:

    • 这行得通。但是,这并不是一个特别好的方法。使用数据绑定控件会是更好的选择。
    • 我同意,也就是说,这不是很好的方法,但是当要求只是呈现表格而没有数据绑定控件提供的任何其他功能时,我更喜欢简单的 html 控件,它将肯定会获得少量的性能。
    • 再多一个忙。我怎样才能在这里对表格进行分页? :) 假设每页 10 条记录
    • 您可以通过两种方式实现。1. 客户端:- 如果表数据没有变大,2. 服务器端:- 如果您不确定数据并且可能存在表中有大量行的机会。基于此,您必须决定方法。
    【解决方案3】:

    在类级别的 aspx.cs 文件中删除一个受保护的表

    protected DataTable dt = new DataTable()
    

    从 sql 查询(使用 DataAdapter)填充这个数据表,我想你知道怎么做,或者只是谷歌它。

    在aspx中,替换

    <tr>                  
        <td><asp:Literal ID="CliNox" runat="server"></asp:Literal></td>      
        <td><asp:Literal ID="CliNamex" runat="server"></asp:Literal></td>            
    </tr>
    

    通过

    foreach (DataRow dr in dt)
    {
        <tr>                  
            <td><%= dr["columnName1"] %></td>    
            <td><%= dr["columnName2"] %></td>            
        </tr>
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多