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

时间:2016-04-21 14:41:03

标签: c# asp.net .net webforms

我有一个 System.Web.UI.WebControls.Table ,其中一个ROW包含五个包含五个服务器控件的单元格。我想点击"添加新行"创建一个新的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

2 个答案:

答案 0 :(得分:2)

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

Test.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>

Test.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>";
        }
    }
}

答案 1 :(得分:1)

我喜欢ryantpayton的方法,但是这里修改了你的代码:

WHERE Product_Id ='