为什么加了一行用FindControl就找不到了?

来源:百度知道 编辑:UC知道 时间:2024/05/18 20:36:21
代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Table tb = new Table();//创建一个表格
tb.ID = "tb1";
tb.BorderWidth = Unit.Parse("0");
tb.Width = Unit.Parse("100%");
if (1==1)
{
for (int j = 0; j < 6; j++)
{
TableRow tr0 = new TableRow();//创建一行

TableCell cell0 = new TableCell();//创建单元格,也就是第一列

cell0.Text = "问题" + (j + 1) + ":";//设置单元格内的文本
tr0.Cells.Add(cell0);//添加到行中
tb.Rows.Add(tr0);

TableRow tr = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "请回答:";
tr.Cells.Add(cell1);

TableCell cell2

因为你创建的这行里没有叫 txtVoteX 的控件呀

---------

最小改动的方法:

foreach (TableRow tr in tb.Rows)
{
int m = i + 1;
TextBox tbox = tr.FindControl("txtVote" + i) as TextBox;
if (tbox != null)
{
Response.Write("txtVote" + m + ": " + tbox.Text + "<br>");
}
i++;
}