我下面的代码目的是动态显示表格tabel,并且提取表格中的输入的数据。点击按钮后在TextBox1中显示tabel的行数。但是TextBox1显示的值是0,改怎样才能提取tabel中的行数呢?
namespace CC.WebModules.Project.Web
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : CC.ThePDM.Web.PDMPage 
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Table table;
string projectID="测试项目";
int zhibiao_Count=4;
protected System.Web.UI.WebControls.TextBox TextBox1;
                  DataSet ds_ProjectZhiBiao;
private void CreateTable()
{
TableRow tr=new TableRow();
TableCell tc_11=new TableCell();
Label lbl=new Label();
TextBox tb=new TextBox();
tc_11.Text="矩阵";
tc_11.Width=Unit.Parse("150px");
tc_11.Wrap=false;
tr.Controls.Add(tc_11);//添加第一行的第一个列
for(int i=0;i<zhibiao_Count;i++)//添加第1行的后zhibiao_Count列
{
TableCell tc_temp=new TableCell();
tc_temp.Text="第"+i+1+"列";
tc_temp.Width=Unit.Parse("60px");
tr.Controls.Add(tc_temp);
}
table.Controls.Add(tr);//添加第一行
for(int i=0;i<zhibiao_Count;i++)//添加后zhibiao_Count行
{
TableRow tr_temp=new TableRow();
TableCell tc_1col=new TableCell();
tc_1col.Text="第"+i+1+"行";
tc_1col.Width=Unit.Parse("150px");
tc_1col.Wrap=false;
tr_temp.Controls.Add(tc_1col); //添加第一列
for(int j=0;j<zhibiao_Count;j++)//添加第i行的各个列
{
TextBox tb_ij=new TextBox();
tb_ij.Width=Unit.Parse("60px");
TableCell tc_temp=new TableCell();
tc_temp.Controls.Add(tb_ij);
tc_temp.Width=Unit.Parse("60px");
tr_temp.Controls.Add(tc_temp);
}
table.Controls.Add(tr_temp);
}
table.Width=tr.Width;
}
    private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{CreateTable();}
}
        private void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Text=table.Rows.Count.ToString();
                   }
   }
}

解决方案 »

  1.   

    我以前用javascript做过,你这样不好实现的
      

  2.   

    采用动态输出HTML的方式真的太麻烦了,我一般只用它来显示数据就可以了。
    你的问题可以实现。就是在输出时给表增加一个ruat=server。然后再利用循环的方式遍历页面上的所有控件,找到这个表后再利用循环读取每一个单元格的内容。这个可以实现,就是麻烦了点。