Get Controls by ID

 

I have recently come across a requirement in one of the webform asp.net project where we wanted to get the controls by ID rather accessing the control object it self. Following is the solution we came up with.

I wrote a generic method which will search and return the control.

 

  1. private T GetControl<T>(string controlName, int tabIndex ,Control parentControl)
  2.              where T : Control
  3.  
  4.         {
  5.             string name = controlName + "_TabPanel" + tabIndex.ToString();
  6.             Control control = parentControl == null ? FindControl(name) : parentControl.FindControl(name);
  7.  
  8.  
  9.             return (T)control;
  10.         }

Following is the code where we use the method.

  1. GetControl<TextBox>("tbNewForecast", selectedTabIndex, ForecastTab.ActiveTab).Text = "";

 

Hope this helps you.

Happy Coding !!!!

Comments

Popular posts from this blog

Responsive Web Design

Affine Cipher in C#

Contract First Development in WCF 4.5