d developer

d developer

  • NA
  • 13
  • 4.2k

How to find child controls in asp.net dynamically

Jun 4 2015 3:02 PM
Hi,
 
My table rows and cells created dynamically in c#. I need to find a control  based on id.
 
< <form id="form1" runat="server>
 <asp:Table id="table" runat="server">
</asp:Table>
</form> 
 
C# 
 
for (int row = 0; row < 10; row++) 
{
TableRow tr= new TableRow();
tr.ID = string.Format("tr_{0}", row);
for (int cell = 0; cell < 4; cell++) 
{
TableCell tc= new TableCell();
tc.ID = string.Format("tc_{0}{1}", row, cell); 
}
 
table.Rows.Add(tc);
 
 
protected void Button_Click(object sender, EventArgs e)
{
 
foreach (TableRow tr in tblDynamic.Rows)
{
  HyperLink hl = this.FindControl("form1$table$tr_0$tc_03$hypAssign0") as HyperLink;
 tr.Cells[3].Controls.Remove(hl);
}
  
 
 
On the above highlighted I could just remove a hyperlink for row 0 and tablecell 3. I need to make dynamic, in that way it removes the hyperlink if I click row 5 or so on so forth.. Right now I hardcoded but hwo to make that tr_0 as tr_i or something for each row selected and tc_03 something dynamic? 
 
 
 
 

Answers (5)