开发者

How to dynamic adding rows into asp.net table?

How can I add rows in a table from server-side?

  if (!Page.IsPostBack)
  {
      Session["table"] = TableId;
  }
  else
  {
      TableId = (Table)Session["table"];
  }

protected void btnAddinRow_Click(object sender, EventArgs e)
{
      num_row = (TableId.Rows).Count;

      Ta开发者_如何学PythonbleRow r = new TableRow();
      TableCell c1 = new TableCell();
      TableCell c2 = new TableCell();
      TextBox t = new TextBox();

      t.ID = "textID" + num_row;
      t.EnableViewState = true;

      r.ID = "newRow" + num_row;
      c1.ID = "newC1" + num_row;
      c2.ID = "newC2" + num_row;

      c1.Text = "New Cell - " + num_row;
      c2.Controls.Add(t);

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

      TableId.Rows.Add(r);
      Session["table"] = TableId;
}

in debug I found out the number in the "TableID", but the rows are not drawn.

Have you got an idea about this issue? Thanks


I think you are confusing with Dynamic Controls... Incase you are creating each of the row in the Table Dynamically... You need to create it (with the same name) on every page cycle.. Whether it is postback or not...

In simple terms, if you are creating something that is not in the .aspx (Tag) page, in your code... Do it on every postback...

In your code, you are retrieving the Table from the Session, but you are not adding it again to the form... The below line does that.

            this.form1.Controls.Add(TableId);

Here is a working code, for your question... hope this helps. Let me know, if you have any questions... Also you may try DataGrid with object Datasource... That will help you to retain the data on postback, without writing it to DB.

    Table TableId = new Table();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            TableId.ID = "MyTable";
        }
        else
        {
            TableId = (Table)Session["table"];
            this.form1.Controls.Add(TableId);
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        Session["table"] = TableId; 
    }


    protected void btnAddinRow_Click(object sender, EventArgs e)
    {
        int num_row = (TableId.Rows).Count;

        TableRow r = new TableRow();
        TableCell c1 = new TableCell();
        TableCell c2 = new TableCell();
        TextBox t = new TextBox();

        t.ID = "textID" + num_row;
        t.EnableViewState = true;

        r.ID = "newRow" + num_row;
        c1.ID = "newC1" + num_row;
        c2.ID = "newC2" + num_row;

        c1.Text = "New Cell - " + num_row;
        c2.Controls.Add(t);

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

        TableId.Rows.Add(r);
        Session["table"] = TableId;
    }


I'm wondering why would you use a table and not the GridView control that has so much powerful and you could accomplish this easily ??

A GridView can be hooked up with a DataTable as it's own DataSource, so, if you need to create a new row, all you need to do is create a new DataRow into the DataTable and bind it again.

You can use this using javascript (Ajax call) or in code-behind.

DataTable dt = (DataTable)Session["myData"];
DataRow dr = dt.NewRow();
dt.Rows.Add(dt);

myGridView.DataSource = dt;
myGridView.DataBind();

you don't need to worry about ID's as they will alwways be unique, and on save for example, you can easily loop through all rows and do what you need per row, like

public void SaveData()
{
    myBusinessObjectCollection = new myBusinessObjectCollection();

    foreach (GridViewRow row in myGridView.Rows)
    {
        myBusinessObjectCollection.Add(
            new myBusinessObject
            {
                C1 = ((TextBox)row.FindControl("txtNewC1")).Text,
                C2 = ((TextBox)row.FindControl("txtNewC2")).Text
            });
    }

    myBusinessObjectCollection.Save();
}


In ASP.NET controls are recreated each postback, so saving link on table in session doesn't make sense.

Store in session(or viewstate) some structure which describes your table (DataTable or collection of strongly typed objects), add rows in this structure on click button event and rebind table using this structure on OnPreRender event.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜