C#使HTML表重复行

时间:2017-03-17 04:38:55

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

我有这个HTML表格:

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();
}

然后我有了这个问题:

import React from 'react';

export default class Header extends React.Component
{
    constructor(props){
        super(props);
        this.state={buttons:[]}
    }

    showButton(){
        let buttons = this.state.buttons;
        buttons += this.createButton();
        this.setState({buttons: buttons});
    }

    createButton()
    {
        return(
        <div>
        <h1>{this.props.task}</h1>
        <button>Edit</button>

        </div>);
    }
    render(){
        const style = {cursor:'pointer'}
        return(
        <div>
        <div style={style}
        onClick={this.showButton.bind(this)}>
        {this.props.task}
        </div>
        <div>
        {this.state.buttons}
        </div>
        </div>
        );
    }
}

这有效但当我需要返回多行时,这只返回1行,因为我只为表体声明了1。我怎么解决这个问题?提前谢谢。

3 个答案:

答案 0 :(得分:1)

在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() });

答案 1 :(得分:1)

在课程级别

中删除aspx.cs文件中的受保护表
protected DataTable dt = new DataTable()

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

在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>
}

答案 2 :(得分:0)

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

# todo_lists/show.html.erb

<% i = 0 %>
<% @todo_list.todo_items.each do |todo_item| %>

  <script>
    $(document).ready(function() {
      $('.mark<%= i %>').on('click', function(){
        $('.line<%= i %>').toggleClass('stroked');
      });
    });
  </script>

  <div class="row clearfix">
    <div class="complete">
      <i class="fa fa-check mark<%= i %>"></i>
    </div>

    <div class="todo_item">
      <p class="line<%= i %>"><%= todo_item.content %></p>
    </div>

    <div class="trash">
      <%= link_to todo_list_todo_item_path(@todo_list, todo_item.id), 
        method: :delete, data: { confirm: "Are you sure?" }  do %>
        <i class="fa fa-trash"></i>
      <% end %>
    </div> 
  </div>

  <% i += 1 %> 
<% end %>