Professional Documents
Culture Documents
Static Controls are Controls that are dynamically created by the framework. Static controls defined in a
.aspx page as follows:
When the page consisting this control is requested by the browser, the server parses this control code and
adds this control to the page Control Tree.
Now , a dynamic control is a control which is dynamically created by the user and added to the page’s
Control Tree.
Before Proceeding I will tell you how the Page Life Cycle affects states of the dynamic controls.
The Page Life Cycle Comes into Play when we deal with dynamic controls.
ASP.net pages are stateless – to put it in a simple way - they tend to forget their state after every postback.
So The controls that are created dynamically will not have their states saved in view state so that on the
next postback their state may be determined. This may lead to problems like :
In order to avoid such problems, we have to re create the dynamic controls after each postback.So The
best Place to Create the code would be during the Page Load phase, in the function-Page_OnLoad().
a) Server Controls.
b) User Controls.
c) Parsed Controls.
You cannot a user control using the procedure foor a server control instead you should use the following
steps to load the user control that you have created .
Control c = this.LoadControl("~/Text.ascx");
PlaceHolder1.Controls.Add(c);
Here Text.ascx is the user control I have created. This Control is first loaded into c of Type Control and
then it is added into a Place holder which acts as the user controls Parent control.
Note once again that user controls cannot be created dynamically as Server Controls.
Creating a Parsed Control uses the Function ParseControl to convert a string of markup to a Control
dynamically.I will show you how it works,
Control c;
c = this.ParseControl("<asp:TextBox Id=’t’ runat=Server />");
In the next section we will see how to load the created controls on the web page.
For any control to be placed on a web page , it should have a parent control. The parent control could be a
Panel or a Placeholder. The Parent Control may be initialized as a static control.
Note that the all controls are referenced by their IDs so, do not forget to assign unique IDs to all the
controls that you create dynamically.
If you want to create a group of textbox controls dynamically say 5 then your code should look similar to,
Button temp;
for (int i = 0; i <noofbuttons; i++)
{
temp = new Button();
temp.ID = "b" + i; //Assigning an Unique ID to the Button.
PlaceHolder1.Controls.Add(temp); //Add the button to the Parent // // Control..
This Control Array can be used to refer to Controls anywhere in the Code.
Referencing has to be done by IDs of the controls. If you have not assigned IDs to controls while creating
them , the page automatically assigns them a random ID which you will find difficult to utilize in your
code and you may end up sometimes having two controls with same IDs.
ControlType temp; //If the control you are referencing to is a button, the ControlType will be Button
The event handlers can be associated with the respective controls using the “+=” association characters.
Hope you found this useful. Don’t forget to leave your comments and suggestions. You could ask questions if any.