[英]Find Dynamically Added Controls From Gridview in SharePoint
我在用户控件中有一个gridview,并使用占位符动态添加了控件,现在我从网格中找到控件,它给出了控件在网格中不存在的代码如下:
foreach (GridViewRow row in GridView1.Rows)
{
PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder");
TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its give Null
}
谁能回答我
从注释中添加了代码:
foreach (GridViewRow dr in GridView1.Rows)
{ PlaceHolder placeHolder = dr.FindControl("lblPlaceHolder") as PlaceHolder;
TextBox txtTextBox1= new TextBox();
txtTextBox1.Width = 300;
placeHolder.Controls.Add(txtTextBox1);
}
删除占位符,您将能够找到您的控件
并使用网格中的onRowDatabound或onRowCreated事件找到控件。
顺便说一句:为什么您的占位符称为lblPlaceHolder,您是否在其中有一个标签,也许您是在询问错误的contol来查找yr文本框,这就是为什么您得到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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.