简体   繁体   中英

Find Dynamically Added Controls From Gridview in SharePoint

I have a gridview in user control and added the controls dynamically using placeholder now i find the controls from grid it give that controls are not exist in the grid Code is as Following :

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

can anyone has answer plz


Added code from Comments:

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

Remove the placeholder, and You will be able to find your controls

and also find your controls using the onRowDatabound, or onRowCreated event from the grid.


btw: why is your placeholder called lblPlaceHolder, do You have a label in there and maybe you are asking the wrong contol to find yr textbox, thats why you are getting a 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);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM