Home > General Software Development, Uncategorized > Quick Tip: Control.Find(string id) Extension Method

Quick Tip: Control.Find(string id) Extension Method

December 27th 2010 | Jimmy Bosse

Quick Tip: Control.Find(string id) Extension Method

For my current project, I am constantly using FindControl to dynamically locate a child control within a parent control. Instead of constantly typing this:

1: DropDownList dropDownList = employeeListView.FindControl(“regionDropDownList”) as DropDownList;

I created an extention method to prettify my code:

   1: public static class ControlExtensions
   2: {
   3:     public static T Find<T>(this Control control, string id) where T : Control
   4:     {
   5:         return (T) control.FindControl(id);
   6:     }
   7: }

So now I can type this:

1: employeeListView.Find(“regionDropDownList”);

Enjoy!

Jimmy Bosse is a Senior .NET developer and Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: