Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

c# - 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;

      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;
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...