Saturday, May 18, 2013

Find dynamic controls ids in .net using recursive method

recursive method : public Control FindControlRecursive(Control control, string ctrlId) { Control returnControl = control.FindControl(ctrlId); if (returnControl == null) { foreach (Control child in control.Controls) { returnControl = FindControlRecursive(child, ctrlId); if (returnControl != null && returnControl.ID == ctrlId) { return returnControl; } } } return returnControl; } Call above method where you want find the contol in asp page. Calling:

Set field Value:

 protected void SetFieldValue(DataTable dsFields) { for (int i = 0; i < dsFields.Rows.Count; i++) { TextBox txt = (TextBox)FindControlRecursive(this, "txt_" + Convert.ToString(dsFields.Rows[i]["field_id"])); if (txt != null) { txt.Text = Convert.ToString(dsApprovalFields.Rows[i]["field_data]"); } } }

 Get the field value:

   TextBox txt = (TextBox)FindControlRecursive(this, "txt_" + Convert.ToString(dt.Rows[i][field_id]));
                         

No comments:

Post a Comment