Wednesday, June 20, 2007
« Kudos for Comcast | Main | Outlook and RSS »

I have been using this little piece of code to recursively search my control collections for a given control or type of controls and thought that it might be of some use to others. The general purpose utility code is here:

public static void SearchControls(ControlCollection controls, Action<Control> action)

{

    foreach (Control control in controls)

    {

        if (control.HasControls())

        {

            SearchControls(control.Controls, action);

        }

        action(control);

    }

}

 

For example, if you want to search for and Trim() all TextBox controls within a GridView, you could call the routine as follows using an anonymous method:

 

SearchControls(GridViewJobs.Controls, delegate(Control control)

{

    TextBox textBox = control as TextBox;

    if (textBox != null)

    {

        textBox.Text = textBox.Text.Trim();

    }

});

 

If you need to search a GridView footer for a drop down list, you call the code as follows:

 

ControlUtility.SearchControls(gvwOrganizations.FooterRow.Controls, delegate(Control control)

{

    DropDownList dropDownListCategory = control as DropDownList;

    if (dropDownListCategory != null)

    {

        categoryID = int.Parse(dropDownListCategory.SelectedValue);

    }

});

 

 

kick it on DotNetKicks.com   Wednesday, June 20, 2007 8:01:19 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [3]  | 
Saturday, December 10, 2011 1:56:32 AM (Pacific Standard Time, UTC-08:00)
This could not possibly have been more hlpfeul!
Saturday, December 10, 2011 9:31:09 AM (Pacific Standard Time, UTC-08:00)
Saturday, December 10, 2011 5:16:01 PM (Pacific Standard Time, UTC-08:00)
Great post Karen and well explained.A couple of questions from a WPF developer&#8230; firstly I understand that the state model will be added to WPF in the future &#8211; does this mean we should stop using Triggers in our WPF application and use States instead if we want Blend to support skinning our apps? Will WPF&#8217;s inbuilt controls get states added to them?Secondly, WPF already has the Parts model and it&#8217;s recommended by Microsoft to prefix all Parts with the PART_ prefix. Is this still recommended in Silverlight?Oh &#8211; how about in Part2 skinning another control, I&#8217;ve already seen lots of examples on CheckBox skinning!Cheers!Neil

home insurance quotes
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview