You are on page 1of 4

In this article I will be dealing with handling dynamic controls in ASP.net.

Dynamic Controls should be


very carefully dealt keeping in mind the ASP.net page cycle. So I request you all to glance through the
ASP.NET Page Life Cycle topic before proceeding. I shall deal with dynamic controls under the
following headings:

I. Difference between a dynamic control and a static control.


II. Create your own dynamic control.
III. Adding Dynamic Controls to your web page.
IV. Referencing the Controls.
V. Event handlers for dynamic controls.

DIFFERENCE BETWEEN DYNAMIC CONTROLS AND STATIC CONTROLS

Static Controls are Controls that are dynamically created by the framework. Static controls defined in a
.aspx page as follows:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

This is a TextBox Server Static Control.

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 :

a) The Control’s event not firing.


b) The Control ‘s state is lost or set to square one on each postback, for example – a
Dropdownlist may looses all its stored values after a postback.
c) And many problems regarding the states of the controls before and after a postback.

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

There are three types of controls that can be created dynamically:

a) Server Controls.
b) User Controls.
c) Parsed Controls.

I will show you how these controls are created dynamically.


CREATING DYNAMIC CONTROLS

Creating a Server Control dynamically :

Button b = new Button();

Creating a User Control Dynamically.

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

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.

ADDING DYNAMIC CONTROLS TO YOUR 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.

The code should be very simple.

Button b = new Button();


b.ID = "button1";
PlaceHolder1.Controls.Add(b);

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

You can do this by using a Control Array too…

Control[] c=new Control [5];


Button b;
for (int i = 0; i < 5; i++)
{
b = new Button();
b.ID = "b" + i;
c[i] = b;
}
for
(int j = 0; j < c.Length ; j++)
{
PlaceHolder1.Controls.Add(c[j]);
}

This Control Array can be used to refer to Controls anywhere in the Code.

REFERENCING THE CONTROLS

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.

The Code for reference to a control inside a placeholder is as follows :

ControlType temp; //If the control you are referencing to is a button, the ControlType will be Button

Temp = (ControlType)Placeholder.FindControl(ID of the Control);


EVENT HANDLERS FOR DYNAMIC CONTROLS

The event handlers can be associated with the respective controls using the “+=” association characters.

Button b = new Button();


b.Click += new EventHandler(b_Click);

Hope you found this useful. Don’t forget to leave your comments and suggestions. You could ask questions if any.

You might also like