如何动态添加行到asp.net表? [英] How to dynamic adding rows into asp.net table?

查看:111
本文介绍了如何动态添加行到asp.net表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在一个表从服务器端添加行?

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

在调试我发现,在表格ID的数目,但该行不会绘制。

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...

在简单来说,如果你正在创建的东西是不是在.aspx(标签)页面,在你的code ...做它在每一个回发...

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

在您的code,你是从会话检索表,但你不重新添加到窗体...下面的行做到这一点。

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

下面是一个工作code,对于您的问题...希望这有助于。让我知道,如果你有任何问题,...此外,你可以尝试使用DataGrid的DataSource对象...这将帮助你保持在回发的数据,而其写入数据库。

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

这篇关于如何动态添加行到asp.net表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆