aspx页面里一个输入行数的textbox,点击AddItem触发AddItem_Click后台事件给table1增加N行表格.
  行数:<asp:TextBox ID="rowsno" runat="server" Width="20px" /><asp:Button ID="AddItem" runat="server" Text=" Add " OnClick="AddItem_Click" />
 <asp:Table ID="table1" runat="server" Width="100%"  style="border:1px solid #ccc">
 </asp:Table>protected void AddItem_Click(object sender, EventArgs e)
    {
        //生成输入的rowsno个数的行
        RowsNo = (rowsno.Text != null) ? Convert.ToInt32(rowsno.Text.ToString()) : 0;
        //RowsNo = Convert.ToInt32(rowsno.Text.ToString());
        //取值时判断行数
        if (RowsNo <= 0)
        {
            KBPublic.MsgBox("输入的参数应该大于1", this.Page);
            KBPublic.GoToPage(this.Request.Url.ToString(), "_self", this.Page);
        }
        else
        {
                #region
                table1.Rows.Clear();
                for (int i = 1; i <= RowsNo; i++)
                {
                    //表1
                    //第一行
                    TableRow tr = new TableRow();
                    TextBox tb1 = new TextBox();
                    TextBox tb2 = new TextBox();
                    TableCell tc1 = new TableCell();
                    tc1.Text = "客户PN码(产品编码)";
                    tr.Cells.Add(tc1);
                    ....                    table1.Rows.Add(tr2);
                }
                #endregion
                DWBind();
                Response.Write(RowsNo);
        }
    }输入成功后.我在后台循环读他们的数据
        try
        {
            for (int i = 1; i <= RowsNo; i++)
            {
                if (this.Page.FindControl("cupn" + i.ToString()) == null) KBPublic.MsgBox("找不到cupn" + i.ToString(), this.Page);
                string CustomerPN = ((TextBox)this.Page.FindControl("cupn" + i.ToString())).Text != null ? ((TextBox)this.Page.FindControl("cupn" + i.ToString())).Text.ToString() : "";
i=1的时候没有问题,但到i=2的时候就出现说找不到对象之类的错误.我调试发现是
this.Page.FindControl("cupn2") == null
也就是找不到cupn2,但我查看源代码.明明是有cupn2的.不知道这是为什么?可能我说的很罗嗦.一句话就是:为什么读不到动态添加的textbox值?3Q了

解决方案 »

  1.   

    页面生命周期问题,
    http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx?mfr=true
    http://blog.csdn.net/sysbug/archive/2007/06/04/1638013.aspx
      

  2.   

    你把控件加到table1中了?
    tc1.Controls.add(控件id);
      

  3.   

    dxphero(火鸟hero) ( ) 信誉:100 
    我把控件textbox添加到表格的td中.但表格也是动态添加的closetome(即鹿无虞,惟入于林中。君子几,不如舍。往吝。) ( ) 信誉:100 
    生命周期???不太了解.能否说详细一点?