Dynamic Controls

  • Uploaded by: P Clement Lloyd
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Dynamic Controls as PDF for free.

More details

  • Words: 877
  • Pages: 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. II. III. IV. V.

Difference between a dynamic control and a static control. Create your own dynamic control. Adding Dynamic Controls to your web page. Referencing the Controls. 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:

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("");

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(); //Assigning an Unique ID to the Button. temp.ID = "b" + i; 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.

Related Documents

Dynamic Controls
June 2020 4
Controls
August 2019 42
Controls
November 2019 32
Controls
November 2019 32
Dynamic
June 2020 20
Dynamic
June 2020 27

More Documents from ""