1. Home
  2. Computing & Technology
  3. Delphi Programming

Developing and Using Custom User Controls in ASP.NET
Manipulating User Control Property Values Programmatically

By Zarko Gajic, About.com

User Control Rendered

Once you have created a user control and specified properties on that user control, you can change those property values both declaratively and programmatically from the page that contains the user control.

Declaratively setting property values for a custom control is quite simple. Unfortunately, programmatically accessing a user control from the Web Forms Page's code-behind involves a few extra steps.

Although Delphi normally adds a member variable corresponding to each control you place on the page so that you can program against the control, it does not do this for user controls. You need to modify the page unit file manually to be able to access its properties and methods.

Accessing a User Control Programmatically

Here are the steps that will enable you to access the ContactInfo user control instance you dropped on a Web Forms page:
  1. Add the name of the unit file where the ContactInfo.ascx class is defined to the uses section of the Web Forms page declaration.
  2. Add a declaration to the strict protected section of the TUserControlTest declaration.
  3. Access the properties of the user control in any of the event handler or methods of the page where you placed an instance of the user control.
Before I show you the code, let's add one more property to the our ContactInfo User Control:
TContactInfo = class(System.Web.UI.UserControl)
...
private
   FEmail : string;
public
   function GetEmail: string;
   procedure SetEmail(const Value: string) ;

   property Email : string read GetEmail write SetEmail;
end;
...
implementation
...
procedure TContactInfo.SetEmail(const Value: string) ;
begin
   emailLabel.Text := '<a href="mailto:' + Value + '">Mail me</a>';
end;

function TContactInfo.GetEmail: string;
begin
   result := FEmail;
end;
The "Email" property turns the emailLabel Label control into a hyperlink (a tag) using the "mailto" protocol.

Now, back to programmatically setting the Email property:

unit UserControlTest;

interface

uses
   ContactInfo, ... ;

type
   TUserControlTest = class(System.Web.UI.Page)
   ...
   strict protected
     UserControl1 : TContactInfo;
   ...
   end;

implementation
...
procedure TUserControlTest.Page_Load(sender: System.Object; e: System.EventArgs) ;
begin
   UserControl1.Email := 'delphi.guide@about.com';
end;
Note: the name (ID property) of our ContactInfo User Control was set to "UserControl1" when we dropped it on the page. You would normally want to rename it.

By now, you know how to add user controls on a Web Forms page at design-time. In real world applications you'll want to be able to dynamically load a user control and place it on the page.

Dynamically Loading User Controls

Just as you can programmatically create an instance of any ASP.NET server control on a Web Forms page, you can do the same with user controls by using the containing page's LoadControl method.

The LoadControl method returns a type of the Control class, and you need to cast the user control to the appropriate strong type to be able to set individual properties on the control.

Here's how to dynamically create an instance of the ContactInfo user control and add it to a Panel object (named Panel1) :

procedure TUserControlTest.Page_Load(sender: System.Object; e: System.EventArgs) ;
var
   contactInfo : TContactInfo;
begin
   contactInfo := Page.LoadControl('~/Controls/ContactInfo.ascx') as TContactInfo;
   contactInfo.Contact := 'Zarko Gajic';
   contactInfo.Email := 'delphi.guide@about.com';

   Panel1.Controls.Add(contactInfo) ;
end;

To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developers

That's it for today. In the next chapter we'll continue exploring the various topic in ASP.NET. If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>
>> Handling Custom User Control Events, View State, Postbacks, ...

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Learn Delphi for .NET
  5. ASP.NET
  6. Developing and Using Custom User Controls in ASP.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.