0

我在用户控件中有一个gridview,并使用占位符动态添加了控件现在我从网格中找到控件,它给出的控件在网格中不存在代码如下:

foreach (GridViewRow row in GridView1.Rows)
{
PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder");
TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its give Null 
}

谁能回答lz


从评论中添加代码:

foreach (GridViewRow dr in GridView1.Rows) 
{ PlaceHolder placeHolder = dr.FindControl("lblPlaceHolder") as PlaceHolder; 
  TextBox txtTextBox1= new TextBox(); 
  txtTextBox1.Width = 300; 
  placeHolder.Controls.Add(txtTextBox1);
} 
4

1 回答 1

0

删除占位符,您将能够找到您的控件

并且还可以使用网格中的 onRowDatabound 或 onRowCreated 事件找到您的控件。


顺便说一句:为什么你的占位符叫做 lblPlaceHolder,你那里有一个标签,也许你要求错误的控制来找到你的文本框,这就是为什么你得到一个 null

void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row as GridViewRow;
    if (row != null)
    {

    TableCell myCell = row.Cells[0] as TableCell
    TextBox txtTextBox1= new TextBox(); 
    txtTextBox1.Width = 300; 
    myCell.Controls.Add(txtTextBox1);

    }
}
于 2012-07-03T05:57:02.697 回答