ASP.NET Web Development with Delphi for .NET

treescattleSoftware and s/w Development

Nov 2, 2013 (4 years and 6 months ago)


ASP.NET Web Development with Delphi for .NET

Bob Swart

In this session, we'll cover ASP.NET Web Development with Delphi for .NET showing how
to design ASP.NET Web Forms, session and state management, authorization and
authentication (login) as well as d

New ASP.NET Application

In this session, we'll use Delphi 2005 to build, debug and deploy an ASP.NET web forms
application, demonstrating the new features and enhancements in Delphi 2005 for
ASP.NET projects. There will be one example applicat
ion, starting right here, that will be
used to demonstrate all new features.

To build the example ASP.NET project, start Delphi 2005

Do File | New and select "ASP.NET Web Application

Delphi for .NET"

Enter "WebAppTutorial" as name, which will produce
a new ASP.NET project in the
WebAppTutorial directory, as specified below:

Note that by default Internet Information Server (IIS) is used. However, Delp
hi 2005
also supports the free Cassini Web Server. Feel free to use Cassini, but refer to the online
help to install and use this open source web server.

If IIS is installed after the .NET Framework, ASP.NET must be registered with IIS by
choosing Start >

Run and entering the following command:

%comspec% /K %windir%


Otherwise, attempting to run an ASP.NET application in the IDE will result in the error
Unable to start debugging on the web server. U
nable to attach to ASP.NET worker
process (typically for aspnet_wp.exe or w3wp.exe)

Click on OK to generate the WebAppTutorial ASP.NET web forms project, which will
create a new WebAppTutorial project with an empty ASP.NET web form called

Save the project, and make sure the form is saved in file Default.aspx, so it will be
shown automatically when visitors go to the virtual directory.

Project Manager

The Delphi 2005 Project Manager has a number of new ASP.NET specific features,
including a

connection with the Object Inspector, built
in support to create new folders or
add other files, and a deployment node.

Check out Project Manager, which should display the project structure as shown below:

If you select a node in the Project Manager, then the Object Inspector will display
information regarding the specific node. Specifically, for the code behind assembly as
well as the source files, you can see the File N
ame and Full Path properties.

Click on the WebForm1.aspx file in the Project Manager. The Object Inspector will display
the File Name (WebForm1.aspx) as well as the Full Path. The latter will be gray,
indicating that you cannot edit it. Change the value o
f the File Name property from
WebForm1.aspx to WebForm42.aspx. This action will change the filename of the ASP.NET
Web Form to WebForm42.aspx. Note that the internal references (in WebForm42.pas as
well as WebForm42.aspx) will also be changed and kept in s

Another connection between the Object Inspector and the Project Manager will be
covered in the Deployment section of this tutorial (which will also cover the Deployment
node in the Project Manager, which can hold deployment configuration settings).

More useful at this time, is the ability to create new directories right from the project
manager. Right
click in the Project Manager on the WebAppTutorial.dll node, and select
the New menu, which has two submenus: Folder and Other.

With New | Folder you
can create a new subdirectory (under the current
WebAppTutorial directory), and with New | Other you can add new
ASP.NET or other Delphi files (class, unit, etc.) to your project:

Select the "Folder" choice of the "New" pop
up menu, and give the new folder the name
"private". The Project Manager now shows a new node in the hierarchy, with the private

In order to add pages to the private directory, you can select it and then right
click to
add new ASP.NET Web Pages, or you can drag existing files from the WebAppTutorial.dll
project and drop them inside the private n
ode. In short, when you right
click on the
private folder node, you can add a new Folder or Other files again. This can be useful, for
example when you want to group pages together (where you can also secure them all at
once). If you've accidentally added
a folder or file you don't want, you can always right
click on it and delete it from the Project Manager (that will also delete the file or folder
from disk). All without having to leave the Delphi 2005 IDE.

We can add any files to the private directory,
but it would be even more interesting to
"safeguard" that directory with some authentication and authorization code.

ASP.NET and Login

In order to add login and authentication support, we need to edit the web.config file and
mark the private directory as
a protected resource for which authorization is required.
This can be done as follows:

<location path="private">



<deny users="?"/>




This will make sure

that no anonymous users (i.e. visitors who are not logged in) can
view pages from that directory. In order to specify the user "Bob" with password "Swart",
you need to add another section to the global <system.web> node in the web.config file,
with the fo
llowing contents:



<authentication mode="Forms">

<forms loginUrl="login.aspx">

<credentials passwordFormat=Clear>

<user name="Bob" password="Swart" />





Note that there already is an <authentication> node, with mode set to Windows, so you
need to replace that node with the new section.

In order to secure the password, you can apply SHA1 (or MD5) encryption, which results
in the fol
lowing final modification of the new <location> section in web.config:



<authentication mode="Forms">

<forms loginUrl="login.aspx">

<credentials passwordFormat="SHA1">

<user name="Bob"

="B0F0B58D128159F8EAFEB02FF53BCCAF0198C232" />





Note that all other pages are not secured, so the authorization and authentication nodes
are only relevant to the Default.aspx page.

Before you can run the
WebAppTutorialASP.NET application, you now first need to build a login page (in file
login.aspx, as specified in the loginUrl attribute).

File | New

, and create a new ASP.NET Page, save this new page in login.aspx.
t a HTML table with 3 rows, 2 columns, and the border size set to 0. Place the text
Username: and Password in the first two cells in the left column, and place two TextBox
controls in the first two cells in the right column, called tbUsername and tbPasswor
d. You
may want to set the TextMode property of the tbPassword control to Password to make
sure no plain text us shown.

Finally, drop a Button called btnLogin on the form, and a Label called lbLogin (with the
Text cleared). You can use this lbLogin label
to display login error messages.

Set the Text property of btnLogin to Login, add the System.Web.Security unit to the uses
clause, and write the following code in the Clic
k event handler:


TWebForm1.btnLogin_Click(sender: System.Object;

e: System.EventArgs);





t, False)


lbLogin.Text := 'Login incorrect!'


You can now compile and run the WebAppTutorialASP.NET application again. When start
the project, you will not directly be taken to the Default.aspx page, but instead to the
login.aspx page
. The URL in the browser will be set to:


After a successful login, you will be redirec
ted to the value of ReturnUrl as specified on
this URL.

HTML Designer and Controls

The Delphi 2005 HTML Designer is enhanced compared to Delphi 8 for .NET, and so is the
way that we can work with the simple HTML elements.

Starting with the HTML elements,

place a Button and TextBox from the HTML elements
category of the Tool Palette on the ASP.NET Web Form.

It's now possible to turn these (and all other) HTML elements into true server controls by
adding the runat="server" attribute to them. This can be do
ne by right
clicking on an
HTML element and selecting the "Run As Server Control" option.

click on the HTML TextBox element, and select "Run As Server Control". Righ
on the HTML Button element, and select "Run As Server Control".

As a result, the ASP.NET Web Form declaration will now have server control editions of
the HTML elements. And the controls themselves will have the runat="server" as well as
an id="..
." attributes added to their tag.

Select the TextBox element, go to the Object Inspector and enter "Hello" as value for the
value property.

HTML Tag Editor

Apart from the Object Inspector, which can be used to edit individual attributes, the
Delphi 2005
HTML Tag Editor now lets you edit the entire tag (not just the inner HTML as

Click on the value attribute for the TextBox element in the HTML Tag Editor, and change
the value from "Hello" to "Hello Delphi 2005".

The HTML tag editor can be used to edit most HTML tags, with the exception of body,
tbody, tr, caption, and tfoot.

Structure View

The Delphi 2005 Structure View (in the upper
left corner of the IDE) shows

the hierarchy
of the HTML in the .aspx page. You can double
click on nodes in the Structure View to be
taken to the corresponding HTML code snippet in the Code Editor.

Click on the WebForm42.aspx tab at the bottom of the code editor so the HTML view is

For the current example, the Structure View is as follows:

click on elements in the HTML Structure View, and note that you'll be taken to
the start of the co
rresponding HTML tag in the HTML Code Editor.

Apart from simple HTML elements, there are more powerful controls, including Borland's
own DB Web controls (which will be covered in a different session, by the way).

State Management

Let's first start with a

little demonstration of ASP.NET state and session support. Add two
TextBox controls to the web page, and call them tbGet and tbSet. Also add two buttons
to the web page, and call them btnGet and btnSet (set their Text properties to Get and

Use the
Set button to put the contents of tbSet in the Application object, and the Get
button to retrieve the contents again and place it in the tbGet TextBox. This is done with
the following code:


TWebForm1.btnGet_Click(sender: System.Object;



tbGet.Text := String(Session['TextBox'])



TWebForm1.btnSet_Click(sender: System.Object;

e: System.EventArgs);


Session['TextBox'] := tbSet.Text


Test this application with two differen
t browser windows, and experience the difference
between the Application and the Session object. Hint: use a checkbox called cbSession to
switch from using the Session object and the Application object at run
time (without
having to stop and restart the ap

This changes the source code for the event handlers as follows:


TWebForm1.btnGet_Click(sender: System.Object;

e: System.EventArgs);




tbGet.Text := String(Session['TextBox'])


tbGet.Text := String(Application['TextBox'])



TWebForm1.btnSet_Click(sender: System.Object;

e: System.EventArgs);




Session['TextBox'] := tbSet.Text


'] := tbSet.Text


The Session object is shared only within the same browser window, but the Application
object is shared among all browser windows, even those on different machines!


Delphi 2005 offers a completely new approach to debuggin
g ASP.Net applications; more
reliable than using C#Builder or Delphi 8 for .NET. This is done by the Borland.dbkasp.dll

a HttpModule, providing improved debugging of ASP.NET applications. During
installation of Delphi 2005, the Delphi 2005 installation p
rogram requests permission to
update your machine.config file with Borland debugger information.

If you answered No to this question, then there are two ways to still enab
le the
Borland.dbkasp.dll. First of all, you can manually edit the web.config file of each ASP.NET
projects. Delphi 2005 places a httpModules node in the web.config file, with the actual
contents in comments, as shown below:





The httpModule Borland.DbkAsp.DbkConnModule is required for debugging under IIS.

This module may be installed in web.config or machine.config, but not both.

Move the following line outside this comment to install.

<add name="Dbg
Connect" type="Borland.DbkAsp.DbkConnModule,Borland.dbkasp,Version=,

Culture=neutral, PublicKeyToken=b0524c541232aae7"/>



If you answered No to the question that would modify the machine.config file, then you
now need to u
ncomment the contents of the httpModules section in your web.config file.

Note that for the Delphi 2005 debugging support, the Borland.dbkasp.dll should be
specified either in the machine.config or in the web.config file, but not in both.

If you want to
verify the contents of the machine.config file, then edit machine.config
from %SystemRoot%
CONFIG. The following entry
should be placed in the section:

<add assembly="Borland.dbkasp, Version=, Culture=neutral,

PublicKeyToken=b0524c541232aae7" />

as well as the following entry in the section:

<add name="DbgConnect"


Culture=neutral, PublicKeyToken=b0524c541232aae7" />

Which should
take care of the debugging.

For more information on ASP.NET Deployment, see my detailed article on this topic at m


In this session, we've cov
ered ASP.NET Web Development with Delphi for .NET showing
how to design ASP.NET Web Forms, session and state management, authorization and
authentication (login) as well as debugging.

Bob Swart

(aka Dr.Bob
) is an independent consultant, trainer,
author & webmaster for Bob Swart Training & Consultancy (eBob42) using Delphi (for
.NET), C#Builder, Kylix, and C++Builder, and has spoken at Delphi and Borland
Developer Conferences since 1993.

Bob is a trainer who has presented all over the world (USA, BeNeLux, Germany, Italy,
Singapore and for UK
BUG in London, Manchester, Reading and Edinburgh). Bob has
written his own training material, includin
g the Delphi 8 for .NET Essentials and Delphi 8
for .NET ASP.NET courseware material licensed by Borland, and the Delphi 2005
courseware manuals available
for sale online

Bob is co
author of th
e Revolutionary Guide to Delphi 2, Delphi 4 Unleashed, C++Builder
4 Unleashed, C++Builder 5 Developer's Guide, Kylix Developer's Guide, Delphi 6
Developer's Guide, and the upcoming C++Builder 6 Developer's Guide, as well as author
for a number of computer
magazines, including The Delphi Magazine, UK
Developer's Magazine, Bitwise Magazine, SDGN Magazine and Blaise, as well as the
IBM DB2 web portal and the Borland Developer Network website, and his own
's Delphi Clinic

Together with Marco Cantù, Bob received the Spirit of Delphi award in 1999 from Borland
at BorCon.


is written in Delphi 2005 using Enterprise Core Objects (ECO) in
mbination with ASP.NET.

This webpage © 2005 by Bob Swart (aka

). All Rights Reserved.