« Home | Dynamics CRM 4.O SDK for IPHONE » | Sql Injection Tracking Tool » | Using LogParser to get Download Details from IIS » | Getting IP Address and ISP name of our Web Page Vi... » | Error Handling in Web.Config » | Handling Application_Error Method » | Error Handling in .net 3.5 » | Using LINQ in String Collections » | SQL in .net Using LINQ » | Querying the DataTable in .Net »

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...

Labels: , , , , ,

Post a Comment