繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 将Delphi作为ASP.NET的脚本语言

将Delphi作为ASP.NET的脚本语言

2006-12-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:将Delphi视为脚本语言 支持ASP.net的第一件事是让ASP.NET将Delphi视为脚本语言,让ASP.NET能够为各种ASP文件类型调用Delphi的.NET编译器。 ASP.NET要在IIS虚路径的根目录下寻找Web.config文件。下面是ASP.NET中使...
关键字:脚本 语言 Delphi NET ASP

将Delphi视为脚本语言

支持ASP.net的第一件事是让ASP.NET将Delphi视为脚本语言,让ASP.NET能够为各种ASP文件类型调用Delphi的.NET编译器。

ASP.NET要在IIS虚路径的根目录下寻找Web.config文件。下面是ASP.NET中使用Delphi作脚本语言的web.config配制文件内容:

type="Borland.Delphi.DelphiCodeProvider,DelphiProvider" />

关于web配制文件的详情请参MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformatofconfigurationfiles.asp

在我的机器(Win2K Pro)上测试Delphi对ASP.NET的支持。要增加一个虚路径来安放Delphi脚本,我把这个路径名定为"vslive"。

在我的机器配制文件上作了这个小改动后就可以来看ASP.NET是如何将Delphi视为脚本语言的。

在ASP.NET中使用Delphi代码

首先编写一个简单应用来确认我们的Delphi支持配制是正确的。下面是editdemo.aspx文件的代码,有一个文本输入框,一个按钮。按钮事件触发显示输入框内容。

可以看到,ASP按钮对象的OnClick事件触发Delphi的ButtonClick过程,而Delphi程序里却使用了ASP对象label Message和text box Edit1。虽然在Delphi过程中没有定义ASP的二个变量,Delphi代码却能找到它们。这是因为脚本中含有的Delphi服务模块能够产生相应的Delphi代码。

现在可以用本地浏览器来浏览这个网页了。因为我把脚本放在"vslive"虚路径,所以打开:

http://localhost/vslive/editdemo.aspx.

以下是编译之后形成的Delphi代码,代码产生WebForm:

//------------------------------------------------------------------------------

//

// 本代码由一个工具生成

// 运行版本:1.0.3705.209

//

// 修改本文件将导致异常行为并丢失生成的代码。

//

//------------------------------------------------------------------------------

unit ASP;

interface

uses System.Collections, System.Collections.Specialized, System.Configuration,

System.Text, System.Text.RegularExpressions, System.Web, System.Web.Caching,

System.Web.SessionState, System.Web.Security, System.Web.UI, System.Web.UI.WebControls,

System.Web.UI.HtmlControls, System.Globalization;

var

editdemo_aspx___autoHandlers: Integer;

editdemo_aspx___intialized: Boolean = False;

editdemo_aspx___fileDependencies: System.Collections.ArrayList;

type

editdemo_aspx = class(System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState)

protected

Edit1: System.Web.UI.WebControls.TextBox;

__control3: System.Web.UI.WebControls.Button;

__control2: System.Web.UI.HtmlControls.HtmlForm;

Message: System.Web.UI.WebControls.Label;

procedure ButtonClick(Sender: System.Object; E: EventArgs);

public

constructor Create;

function get_AutoHandlers: Integer; override;

function get_ApplicationInstance: System.Web.HttpApplication; virtual;

function get_TemplateSourceDirectory: System.String; override;

procedure set_AutoHandlers(Value: Integer); override;

protected

property AutoHandlers: Integer read get_AutoHandlers write set_AutoHandlers;

property ApplicationInstance: System.Web.HttpApplication read get_ApplicationInstance;

public

property TemplateSourceDirectory: System.String read get_TemplateSourceDirectory;

private

function __BuildControlEdit1: System.Web.UI.Control;

function __BuildControl__control3: System.Web.UI.Control;

function __BuildControl__control2: System.Web.UI.Control;

function __BuildControlMessage: System.Web.UI.Control;

procedure __BuildControlTree(__ctrl: System.Web.UI.Control);

protected

procedure FrameworkInitialize; override;

public

function GetTypeHashCode: Integer; override;

end;

implementation

procedure editdemo_aspx.ButtonClick(Sender: System.Object; E: EventArgs);

begin

Message.Text := Edit1.Text;

end;

constructor editdemo_aspx.Create;

var

dependencies: System.Collections.ArrayList;

begin

inherited Create;

if (ASP.editdemo_aspx___intialized = False) then

begin

dependencies := System.Collections.ArrayList.Create;

dependencies.Add('d:\vslive\editdemo.aspx');

ASP.editdemo_aspx___fileDependencies := dependencies;

ASP.editdemo_aspx___intialized := True;

end;

Self.Server.ScriptTimeout := 30000000;

end;

function editdemo_aspx.get_AutoHandlers: Integer;

begin

Result := ASP.editdemo_aspx___autoHandlers;

end;

function editdemo_aspx.get_ApplicationInstance: System.Web.HttpApplication;

begin

Result := Self.Context.ApplicationInstance as System.Web.HttpApplication;

end;

function editdemo_aspx.get_TemplateSourceDirectory: System.String;

begin

Result := '/vslive';

end;

procedure editdemo_aspx.set_AutoHandlers(Value: Integer);

begin

ASP.editdemo_aspx___autoHandlers := Value;

end;

function editdemo_aspx.__BuildControlEdit1: System.Web.UI.Control;

var

__ctrl: System.Web.UI.WebControls.TextBox;

begin

__ctrl := System.Web.UI.WebControls.TextBox.Create;

Self.Edit1 := __ctrl;

__ctrl.ID := 'Edit1';

__ctrl.Width := System.Web.UI.WebControls.Unit.Parse('300px', System.Globalization.CultureInfo.InvariantCulture);

Result := __ctrl;

end;

function editdemo_aspx.__BuildControl__control3: System.Web.UI.Control;

var

__ctrl: System.Web.UI.WebControls.Button;

begin

__ctrl := System.Web.UI.WebControls.Button.Create;

Self.__control3 := __ctrl;

__ctrl.Text := 'Click Me!';

__ctrl.add_Click(Self.ButtonClick);

Result := __ctrl;

end;

function editdemo_aspx.__BuildControl__control2: System.Web.UI.Control;

var

__parser: System.Web.UI.IParserAccessor;

__ctrl: System.Web.UI.HtmlControls.HtmlForm;

begin

__ctrl := System.Web.UI.HtmlControls.HtmlForm.Create;

Self.__control2 := __ctrl;

__parser := __ctrl as System.Web.UI.IParserAccessor;

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10' '));

Self.__BuildControlEdit1;

__parser.AddParsedSubObject(Self.Edit1);

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10' '));

Self.__BuildControl__control3;

__parser.AddParsedSubObject(Self.__control3);

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10' '));

Result := __ctrl;

end;

function editdemo_aspx.__BuildControlMessage: System.Web.UI.Control;

var

__ctrl: System.Web.UI.WebControls.Label;

begin

__ctrl := System.Web.UI.WebControls.Label.Create;

Self.Message := __ctrl;

__ctrl.ID := 'Message';

Result := __ctrl;

end;

procedure editdemo_aspx.__BuildControlTree(__ctrl: System.Web.UI.Control);

var

__parser: System.Web.UI.IParserAccessor;

begin

__parser := __ctrl as System.Web.UI.IParserAccessor;

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10' '));

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10' '#13#10' '));

Self.__BuildControl__control2;

__parser.AddParsedSubObject(Self.__control2);

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create(''#13#10'

'));

Self.__BuildControlMessage;

__parser.AddParsedSubObject(Self.Message);

__parser.AddParsedSubObject(System.Web.UI.LiteralControl.Create('

'#13#10' '#13#10''#13#10));

end;

procedure editdemo_aspx.FrameworkInitialize;

begin

Self.__BuildControlTree(Self);

Self.FileDependencies := ASP.editdemo_aspx___fileDependencies;

Self.EnableViewStateMac := True;

end;

function editdemo_aspx.GetTypeHashCode: Integer;

begin

Result := -764444463;

end;

end.

注意:这里介绍的是Delphi的.NET编译器功能的预览。正式发布的Delphi 7生成的代码与上面将有显著不同。本文仅是示例说明如何在ASP.NET中使用Delphi及其功能。

更高技术的网页

ASP.net的一些控件远比HTML控件要更加智能化。其中之一就是日历(Calendar)控件。

以下代码是calendar.aspx中的一部分。代码提供二种方法让控件设置日期:

 浏览日历然后选择日期;

 按照日期格式输入日期,确认选择。这种方法由.NET的Convert类支持。

Delphi for .NET running in ASP.NET

Please pick a date

OnSelectionChanged="Calendar1Selected">

在日历上选择日期触发OnSelectionChanged事件,调用Delphi的Calendar1Selected()过程。

输入日期,点击"Set data"按钮触发Button1Click事件,调用Delphi的Button1Click()过程。

数据处理

现在通过日历的日期选择来选择显示数据库数据。在这个ASP.NET Delphi网页上,增加一个DataGrid和一个TextBox,前者显示数据库数据,后者输入要显示的数据库域。

<%@Import Namespace="System.Data"%>

<%@Import Namespace="System.Data.SqlClient"%>

<%=ProdName %> with a Calendar, DataGrid, & SqlClient in ASP.NET

Pick a date

OnSelectionChanged="DateSelected">

Display fields:

text="OrderID, CustomerID, ShipName, ShipCity, ShipCountry" width=500 runat="server"/>

每当用户点击日历选择日期时,就触发OnSelectionChanged事件,调用DateSelected()函数。

在GetOrders函数中,数据库的连接由命名空间定义的SqlClient实现,数据库是MS SQL 2000的示范库Northwind。SqlDataAdapter将查询结果安装到DataGrid中,显示出数据表格,如附图。

改变域输入框的域名,再点击日历,就得到不同的结果。

这就是日历驱动的数据库查询系统,由Delphi for .NET与ASP.NET共同完成。

结语

本文试图简略说明在ASP.NET中应用Delphi是如何方便。

请记住,本文示例是Delphi 7的预览示例,正式版本的结果也许不完全如此,当然也许就是如此。

责任编辑:admin
相关文章