简体   繁体   English

如何动态地向表单元格添加行

[英]How to add row to tablecell dynamicaly

I have this: 我有这个:

<asp:Table id="tbl_Items" runat="server">
</asp:Table>
<asp:Button ID="btn_AddNewItemField" runat="server" Text="Add New Item" 
    onclick="btn_AddNewItemField_Click" />

In PageLoad() I add one row: PageLoad()我添加一行:

            TableRow row = new TableRow();

            TableCell c1 = new TableCell();
            c1.Controls.Add(new TextBox());

            TableCell c2 = new TableCell();
            c2.Controls.Add(new DropDownList());

            row.Cells.Add(c1);
            row.Cells.Add(c2);

            this.tbl_Items.Rows.Add(row);

And this works. 这可行。
But when I click on button to add new row I call this same code and code goes without errors but nothing happens. 但是,当我单击按钮以添加新行时,我调用了相同的代码,并且代码没有错误但没有任何反应。 No error, no row, no nothing. 没有错误,没有行,什么都没有。 What am I doing wrong? 我究竟做错了什么?

What you need is a little State management try this 您需要的是一点点的国家管理,请尝试一下

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            AddRow(true);
        else
            AddRows();
    }
    protected void btn_AddNewItemField_Click(object sender, EventArgs e)
    {
        AddRow(true);
    }
    void AddRow(bool addCounter)
    {
        TableRow row = new TableRow();

        TableCell c1 = new TableCell();
        c1.Controls.Add(new TextBox());

        TableCell c2 = new TableCell();
        c2.Controls.Add(new DropDownList());

        row.Cells.Add(c1);
        row.Cells.Add(c2);

        this.tbl_Items.Rows.Add(row);

        if (addCounter)
        {
            if (ViewState["rowCount"] == null)
                ViewState["rowCount"] = 1;
            else
            {
                int count = ((int)ViewState["rowCount"]);
                ViewState["rowCount"] = ++count;
            }
        }
    }
    void AddRows()
    {
        if (ViewState["rowCount"] == null)
            return;
        int count = ((int)ViewState["rowCount"]);
        for (int i = 1; i <= count; i++)
        {
            AddRow(false);
        }
    }

EDIT: The position will not change anything i think you have some other error; 编辑:该位置不会改变任何东西,我认为您还有其他错误; try this (this works for me): 试试这个(这对我有用):

<asp:Table id="tbl_Items" runat="server"></asp:Table>
<asp:Button ID="btn_AddNewItemField" runat="server" Text="Add New Item" />

    protected void Page_Load(object sender, EventArgs e)
    {
        TableRow row = new TableRow();

        TableCell c1 = new TableCell();
        c1.Controls.Add(new TextBox());

        TableCell c2 = new TableCell();
        c2.Controls.Add(new DropDownList());

        row.Cells.Add(c1);
        row.Cells.Add(c2);

        this.tbl_Items.Rows.Add(row);

        btn_AddNewItemField.Click += new EventHandler(btnAddNewItemFieldClick);
    }

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

what is the code of "Page_Load"? “ Page_Load”的代码是什么? This is probably due to a problem of "postback". 这可能是由于“回发”问题造成的。 At one point the code returns to the "Page_load" function resets when you click a button.. 在某一时刻,单击按钮,代码将返回到“ Page_load”功能重置。

Tried to include your code with "if (! IsPostBack) {}" in your Page_Load function to avoid problems. 试图在您的Page_Load函数中将代码包含“ if(!IsPostBack){}”,以避免出现问题。

like this.. 像这样..

Public void Page_Load()
{
     if(!IsPostBack)
     {

        //....Your code
     }

}

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

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