开发者

Dynamically Add/Remove table rows in ASP.NET

Does anyone know how to dynamically add and remove rows in a table triggered by a button click from the backend (in c#) using asp.net?

Here's how it might be done in javascript, is the开发者_开发技巧re any way to do this in the asp.net framework?

http://viralpatel.net/blogs/2009/03/dynamically-add-remove-rows-in-html-table-using-javascript.html


In the event handler for your button:

  1. Open a connection to the database which contains the table you wish to modify.
  2. If you want to add a row, execute an INSERT statement (or a stored procedure which INSERTs). If you want to remove a row execute a DELETE statemenet (or, etc.).
  3. Close the database connection.

Your table should be modified. Once you master this kind of stuff, I would recommend you look at an OR Mapper like Entity Framework or NHibernate, which will provide a layer for managing this kind of stuff in a more efficient way.


Build your table from the code behind. You will be able to do whatever you want that way. something like that, not sure about the class names:

var table = new Table();
var row = new TableRow();
table.Controls.Add(row);
var cell = new TableCell();
row.Controls.Add(cell);
page.Controls.Add(table);


try this, it worked for me

           HtmlTable tbl = (HtmlTable)pnl.FindControl("tblDataFeed");

            for (int ix = 0; ix <= tbl.Rows.Count - 1; ix++)
            {
                HtmlTableRow row = tbl.Rows[ix];
                tbl.Rows.Remove(row);
            }

or this

            foreach (HtmlTableRow inRow in tbl.Rows)
            {
               tbl.Rows.Remove(inRow);
            }


You can use this code to remove row from table on button click.

 protected void btnRemove_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string bid = btn.ID;       
        Table tl = (Table)panel.FindControl("tal");
        for (int i = 1; i < tbl.Rows.Count; i++)
        {
            TableRow row = (TableRow)tl.Rows[i];
            string id = "lnk" + (i-1).ToString();
            if (bid == row.Cells[2].FindControl(id).ID)
            {
                tbl.Rows.Remove(row);
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜