c# - ASP.NET - 在表中动态添加/删除 TableRow

标签 c# asp.net .net webforms

我有一个 System.Web.UI.WebControls.Table,其中一个 ROW 包含五个单元格和五个服务器控件。我想在单击“添加新行”时创建一个新行以及如何在第一行单元格上设置名称?

如何做到这一点?请给我类似的教程。

这是我的用户界面: enter image description here

这是我的代码:

public class HelloWorldWeb : WebPart
{
    protected override void CreateChildControls()
    {
        //table Cells Controls
        TextBox txt1 = new TextBox();
        txt1.Height = 19;
        TextBox txt2 = new TextBox();
        txt2.Height = 19;
        TextBox txt3 = new TextBox();
        txt3.Height = 19;
        DateTimeControl dt1 = new DateTimeControl();
        dt1.DateOnly = true;
        DateTimeControl dt2 = new DateTimeControl();
        dt2.DateOnly = true;
        Button btnAdd = new Button();
        btnAdd.Text = "Create New Row";
        btnAdd.Click += new EventHandler(btnAdd_Click);


        //Declaration one Row and Five Cells(with controls)
        Table myTbl = new Table();
        TableRow tRow = new TableRow();

        TableCell tCellOne = new TableCell();
        tCellOne.Controls.Add(txt1);
        tRow.Cells.Add(tCellOne);

        TableCell tCellTwo = new TableCell();
        tCellTwo.Controls.Add(dt1);
        tRow.Cells.Add(tCellTwo);

        TableCell tCellThree = new TableCell();
        tCellThree.Controls.Add(dt2);
        tRow.Cells.Add(tCellThree);

        TableCell tCellFour = new TableCell();
        tCellFour.Controls.Add(txt2);
        tRow.Cells.Add(tCellFour);

        TableCell tCellFive = new TableCell();
        tCellFive.Controls.Add(txt3);
        tRow.Cells.Add(tCellFive);

        myTbl.Rows.Add(tRow);

        this.Controls.Add(new LiteralControl("<table class='ms-formbody' vAlign='top' >"));

        this.Controls.Add(new LiteralControl("<tr>"));
        this.Controls.Add(new LiteralControl("<td width='100' class='ms-formlabel' noWrap='nowrap' vAlign='top'>"));
        this.Controls.Add(myTbl);
        this.Controls.Add(btnAdd);
        this.Controls.Add(new LiteralControl("</td>"));
        this.Controls.Add(new LiteralControl("</tr>"));

        this.Controls.Add(new LiteralControl("</table>"));

        base.CreateChildControls();
    }

    void btnAdd_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

结果必须是:

enter image description here

最佳答案

我不确定您正在从事什么类型的项目,但使用简单的 Web 表单我可以实现此目的:

测试.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Stackoverflow.Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
        <div>
            <table class="ms-formbody" valign="top">
                <tbody>
                    <asp:Literal ID="litTest" runat="server"></asp:Literal>
                </tbody>
            </table>

            <asp:Button ID="btnAdd" runat="server" Text="Create New Row" OnClick="btnAdd_Click" />
        </div>
    </form>
</body>
</html>

测试.aspx.cs:

using System;
using System.Web.UI;

namespace Stackoverflow
{
    public partial class Test : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                AddRow();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            AddRow();
        }

        private void AddRow()
        {
            litTest.Text += "<tr><td><input type=\"text\" style=\"height: 19px;\"></td><td><input type=\"text\" style=\"height: 19px;\"></td><td><input type=\"text\" style=\"height: 19px;\"></td></tr>";
        }
    }
}

关于c# - ASP.NET - 在表中动态添加/删除 TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36772957/

相关文章:

c# - Entity Framework 计数效率

c# - 为什么我的必需属性没有在 Entity Framework Core 中级联删除?

c# - 从 Linq byte[] Concat 转换为旧的 .net Array 并考虑复制和长度

C#- "Object reference not set to an instance of an object"

javascript - 将 Base64 图像发布到 Mvc Controller

c# - Radgrid 自定义插入模板

asp.net - 将静态内容从 ASP.NET 项目迁移到 Windows Azure 平台

c# - 反射: How to get base method (not original method)?

c# - 如何在wpf treeview中显示所有直接和间接子成员的数量

c# - 删除 xaml 文件中未使用的引用程序集