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.
- private T GetControl<T>(string controlName, int tabIndex ,Control parentControl)
- where T : Control
- {
- string name = controlName + "_TabPanel" + tabIndex.ToString();
- Control control = parentControl == null ? FindControl(name) : parentControl.FindControl(name);
- return (T)control;
- }
Following is the code where we use the method.
- GetControl<TextBox>("tbNewForecast", selectedTabIndex, ForecastTab.ActiveTab).Text = "";
Hope this helps you.
Happy Coding !!!!
Comments
Post a Comment