简体   繁体   English

动态将行添加到HTML表

[英]Adding rows dynamically to a HTML table

I have a asp.net web site that contains some tables. 我有一个包含一些表的asp.net网站。 However these are not asp:Table ie they are simple HTML tables built this way: 但是这些不是asp:Table,即它们是以这种方式构建的简单HTML表:

 <table><tr><td></td></tr></table> 

Now I would like to know if it's possible to add rows dynamically to this table from the code behind (ie C#) and if yes, how do I go about doing it? 现在,我想知道是否可以从后面的代码(即C#)向该表动态添加行,如果可以,我该如何去做?

you can give it an Id and Set runat="server" attribute and use it from code behind using Id you gave it 您可以给它一个Id并设置runat =“ server”属性,并使用您给它的Id从后面的代码中使用它

System.Web.UI.HtmlControls.HtmlTableRow r=new System.Web.UI.HtmlControls.HtmlTableRow();
            System.Web.UI.HtmlControls.HtmlTableCell c = new System.Web.UI.HtmlControls.HtmlTableCell();
            c.InnerText = "New Cell";
            r.Cells.Add(c);
            T.Rows.Add(r);

where T is the Id of your table 其中T是表的ID

It's been a while but there is the repeater-control in ASP.NET Webforms for this kind of stuff. 已经有一段时间了,但是ASP.NET Webforms中有用于此类内容的中继器控件。

Here is a nice article introducing this conept: Data Repeater Controls in ASP.NET 这是一篇介绍这一概念的不错的文章: ASP.NET中的数据转发器控件

I think this will be easier for you than hacking this with AJAX/JScript 我认为这比使用AJAX / JScript进行黑客攻击更容易

Aside from this: Daniel Casserly is right - this is very easy if you do it in MVC (even easier if you use Razor-Syntax), as it would translate do something like: 除此之外:Daniel Casserly是正确的-如果您在MVC中进行操作,这非常容易(如果使用Razor-Syntax,则更加容易),因为它将转换为类似以下内容:

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Name</td>
            ... whatever you need here ...
        </tr>
    }
</table>

您可以使用JQuery进行此操作,但为此您需要提供ID和ID或类,或者仅搜索表数组,然后向其中注入行

I think its better to create the entire HTML table from the code behind. 我认为最好从后面的代码创建整个HTML表。 For this we can add a literal and use it in the code behind to add a table structure from the code behind. 为此,我们可以添加一个文字,并在后面的代码中使用它,以便从后面的代码中添加表结构。

eg Lets make an HTML table of Birds detail. 例如,让我们制作一个HTML表格的Birds详细信息。 Just to keep explanation short I have not included the data table part filled with birds detail. 为了使说明简短,我没有包括充满鸟类细节的数据表部分。

if (dtBirdsDetail.Rows.Count > 0)
    {
      litBirdsTable.Text = "<center><table id='tbldata' cellspacing='0' cellpadding='1' border='1' style='border-collapse: collapse;'>" + System.Environment.NewLine;
      litBirdsTable.Text += "<tr>";

      //add datatable columns to html table as heading
for (int liColumnIndex = 0; liColumnIndex < dtBirdsDetail.Columns.Count;liColumnIndex++)
       {
         litBirdsTable.Text += "<th>" + dtBirdsDetail.Columns[liColumnIndex].ColumnName 
                             + "</th>" +   System.Environment.NewLine;
       }
       litBirdsTable.Text += System.Environment.NewLine + "</tr>";

       //add datatable rows to html table
       for (int liRowIndex = 0; liRowIndex < dtBirdsDetail.Rows.Count; liRowIndex++)
       {
         litBirdsTable.Text += "<tr>";
         litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["ID"] + "</td>" +              
                                System.Environment.NewLine;
         litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["BirdName"] + " 
                               </td>" + System.Environment.NewLine;
         litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["TypeOfBird"] + " 
                               </td>" + System.Environment.NewLine;
         litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["ScientificName"] 
                             + "</td>" + System.Environment.NewLine;
         litBirdsTable.Text += "</tr>";
       }
        litBirdsTable.Text += "</table></center>";
    }

For detailed explanation, visit this link: 有关详细说明,请访问此链接:

http://codevariation.blogspot.com/2018/03/html-table-design-with-dynamic-data.html http://codevariation.blogspot.com/2018/03/html-table-design-with-dynamic-data.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM