C# 从SharePoint中的Gridview中查找动态添加的控件
我在用户控件中有一个gridview,并使用占位符动态添加控件 现在我从网格中找到控件,它给出的控件在网格中不存在 代码如下:C# 从SharePoint中的Gridview中查找动态添加的控件,c#,sharepoint,sharepoint-2010,C#,Sharepoint,Sharepoint 2010,我在用户控件中有一个gridview,并使用占位符动态添加控件 现在我从网格中找到控件,它给出的控件在网格中不存在 代码如下: foreach (GridViewRow row in GridView1.Rows) { PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder"); TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its giv
foreach (GridViewRow row in GridView1.Rows)
{
PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder");
TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its give Null
}
有人能回答plz吗
从注释中添加代码:
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,你有标签吗?也许你要求错误的控件找到你的文本框,这就是为什么你得到一个空值
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);
}
}
谢谢,先生,我已经试过了,但仍然给出空值OK,plc有值还是也有空值?plc有值它不是空值您在它上面放了一个断点并查看了控件属性,以确保其中有项。i、 e.计数?FindControl不会递归到控件的子控件中;它只是检查直接的孩子。也许你想实现自己的深度查找。我在占位符中动态添加了控件如何在gridview中动态添加控件而不使用占位符?不,它只是一个名称lblPlaceHolder thare是占位符中的文本框,我动态添加了它,我也在输出中获得了它,但当我想进入按钮时,请单击它的give nullWithRow.Controls.Add我遇到了这个错误:-“TableRow”不能有“TextBox”类型的子项。我添加了一个代码示例,使用Row.Cells[0]我指的是网格的第一列,如果您使用的是另一列,请设置该列的正确索引Hi Johnn,谢谢我添加了您的代码,工作正常。现在你能告诉我如何找到点击按钮的控件吗?