Working with Dynamic Controls
Accessing dynamic controls created on the page is a bit tedious job. But with the help of generics we can actually access n number of dynamic controls created on the page.
public T FindControl(Control container, string id) where T : Control
{
return (container.FindControl(id) as T);
}
This function takes the id of the control we want to find and the control it's in, casts it to the type you specify and returns.
Example :
TextBox txtBox = FindControl(this.wrapper, "mytextbox");
This can be further expanded, by creating property for multiple methods.
private TextBox _MyTextBox;
public TextBox DynamicTextBox
{
get
{
if (_MyTextBox == null)
{
_MyTextBox = FindControl(this.wrapper, "mytextbox");
}
return _MyTextBox;
}
}
Now we can simply use the dynamic controls like anyother controls in the page.
Label1.Text = this.mytextbox.Text.ToString();
Happy Coding...
public T FindControl
{
return (container.FindControl(id) as T);
}
This function takes the id of the control we want to find and the control it's in, casts it to the type you specify and returns.
Example :
TextBox txtBox = FindControl
This can be further expanded, by creating property for multiple methods.
private TextBox _MyTextBox;
public TextBox DynamicTextBox
{
get
{
if (_MyTextBox == null)
{
_MyTextBox = FindControl
}
return _MyTextBox;
}
}
Now we can simply use the dynamic controls like anyother controls in the page.
Label1.Text = this.mytextbox.Text.ToString();
Happy Coding...
Labels: .net2.0, .net3.5, accessing, controls, dynamic controls, generic
Post a Comment