Posts

Showing posts from 2009

Using reflection to find property of a control in asp.net 2.0

The following method is used to find control's Enabled property and set the value (true/false) using reflection. You can find any property of the control by specifying property name in GetProperty() method. public static void SetEnabled(object control, bool flag) { try { if (control == null) throw new ArgumentNullException("control", "control is null"); Type itemType = control.GetType(); System.Reflection.PropertyInfo enabledProperty= itemType.GetProperty("Enabled",BindingFlags.Instance | BindingFlags.Public); if (enabledProperty != null) { enabledProperty.SetValue(control, flag, null); } else { throw new ArgumentException("control does not have an \"Enabled\" property", "control"); } } catch (Exception ex)

Recursive function to find controls & child controls in asp.net 2.0

The following method can be used to find controls and their child controls with help of recursion. public void FindControlsRecursively(Control oControl) { foreach (Control frmCtrl in oControl.Controls) { Response.Write(frmCtrl.GetType().ToString() + " "); if (frmCtrl.HasControls()) { FindControlsRecursively(frmCtrl); } } }

Finding controls thru iteration in asp.net 2.0

This example shows how to find control from a page which is inside a master page. public void FindControls(ControlCollection ctlCollection) { foreach (Control ctl in ctlCollection) { if (ctl.ID == "TextBox") { //... some code here } } } and add the following code in your page (which is inside master page). ContentPlaceHolder cph = (ContentPlaceHolder)this.Form.FindControl("ContentPlaceHolder1"); findControls(cph.Controls);