ASP在表中动态添加和删除TableRows(带有服务器控件)

时间:2016-04-22 06:33:03

标签: c# asp.net .net webforms

我的解决方案是类库,我想创建包含动态行的表(我的约束最多10行)。这行必须是从我的表中添加或删除

这是mi UI:

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

0 个答案:

没有答案