To use Delphi as the scripting language for ASP.net

Source: Internet
Author: User
Tags date config constructor integer query tostring domain name root directory
asp.net| Script

View Delphi as a scripting language

The first thing to support asp.net is to let ASP.net see Delphi as a scripting language, allowing ASP.net to invoke the Delphi. NET compiler for various ASP file types.

Asp. NET to find the Web.config file under the root directory of the IIS virtual path. The following is the contents of the Web.config configuration file using Delphi as a scripting language in asp.net:

<configuration>
<system.web>
<compilation debug= "true" >
<assemblies>
<add assembly= "Delphiprovider"/>
</assemblies>
<compilers>
<compiler language= "Delphi" extension= ". Pas"
Type= "Borland.delphi.delphicodeprovider,delphiprovider"/>
</compilers>
</compilation>
</system.web>
</configuration>

For more information about Web configuration files, please refer to MSDN:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformatofconfigurationfiles.asp

Test Delphi's support for ASP.net on my machine (Win2K Pro). To add a virtual path to the Delphi script, I set the pathname to "VSLive".

After making this small change on my machine configuration file, I can see how ASP.net sees Delphi as a scripting language.

Using the Delphi code in asp.net
First, write a simple application to confirm that our Delphi support configuration is correct. Here is the code for the Editdemo.aspx file, which has a text input box and a button. The button event triggers the Display input box contents.

<script language= "Delphi" runat= "Server" >
Procedure ButtonClick (Sender:System.Object; E:eventargs);
Begin
Message.Text: = Edit1.text;
End
</script>
<body>
<form runat= "Server" >
<asp:textbox id= "Edit1" runat= "Server"/>
<asp:button text= "click me!" runat= "Server"/>
</form>
<p><b><asp:label id= "message" runat= "Server"/></b></p>
</body>

You can see that the ASP button object's onclick event triggers the buttonclick process of Delphi, while the Delphi program uses the ASP object label message and text box Edit1. Although in the Delphi process does not define the ASP two variables, Delphi code can find them. This is because the script contains the Delphi Service module can produce the corresponding Delphi code.

Now you can use the local browser to browse this page. Because I put the script on the "vslive" virtual path, open:
Http://localhost/vslive/editdemo.aspx.

The following is compiled after the Delphi code, code generation WebForm:

//------------------------------------------------------------------------------
<autogenerated>
This code is generated by a tool
Run Version: 1.0.3705.209
//
Modifying this file will result in abnormal behavior and loss of generated code.
</autogenerated>
//------------------------------------------------------------------------------

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 (' __parser. AddParsedSubObject (System.Web.UI.LiteralControl.Create (' #13 #10 ' <body> ' #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 (' </b></p> ' #13 #10 ' </body> ' #13 #10 ' </ Html> ' #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.

Note: This is a preview of the Delphi. NET compiler feature. The officially released Delphi 7 code generated will be significantly different from the above. This article is just an example of how to use Delphi and its functions in asp.net.

Higher-tech Web pages
Some of the ASP.net controls are far more intelligent than HTML controls. One of these is the Calendar control.

The following code is part of the calendar.aspx. The code provides two ways for the control to set the date:
Browse the calendar and select a date;
Enter a date in date format to confirm the selection. This method consists of. NET for convert class support.

<script language= "Delphi" runat= "Server" >
Procedure calendar1selected (Sender:System.Object; E:eventargs);
Begin
Label1.Text: = ' Delphi for. NET says you picked ' + Calendar1.SelectedDate.ToString (' D ');
End

Procedure Button1Click (Sender:System.Object; E:eventargs);
Begin
Calendar1.visibledate: = System.Convert.ToDateTime (Edit1.text);
Label1.Text: = ' Delphi for. NET says you set ' + Calendar1.VisibleDate.ToString (' D ');
End
</script>

<body style= "font:18pt Verdana" >
<form runat= "Server" >
<center>
<p>please Pick a date</p>
<asp:calendar id= "Calendar1" runat= "Server" forecolor= "#0000FF" backcolor= "#FFFFCC"
onselectionchanged= "calendar1selected" >
<todaydaystyle font-bold= "True"/>
<nextprevstyle forecolor= "#FFFFCC"/>
<dayheaderstyle backcolor= "#FFCC66"/>
<selecteddaystyle forecolor= "Black" backcolor= "#CCCCFF"/>
<titlestyle font-size= "14pt" font-bold= "True" forecolor= "#FFFFCC" backcolor= "#990000"/>
<othermonthdaystyle forecolor= "#CC9966"/>
</asp:Calendar>
<p><asp:textbox id= "Edit1" width=200 runat= "Server"/>
<asp:button text= "Set date" id= "Button1" runat= "Server"/>
</p>
<p><asp:label id= "Label1" runat= "Server"/></p>
</center>
</form>
</body>

Select the date on the calendar to trigger the OnSelectionChanged event and invoke the Delphi calendar1selected () procedure.
Enter the date, click the "Set Data" button to trigger the Button1Click event, call the Delphi Button1Click () process.

Data processing
Now choose to display the database data through the calendar's date selection. On this asp.net Delphi page, add a DataGrid and a textbox that displays the database data, which enters the database field to display.

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

<script language= "Delphi" runat= "Server" >
Const
ProdName = ' Delphi for. NET ';
Dispfields = ' OrderID, CustomerID, ShipName, ShipCity, ShipCountry ';

Procedure dateselected (Sender:System.Object; E:eventargs);
Begin
Label1.Text: = prodname + ' says you picked ' + Calendar1.SelectedDate.ToString (' D ');
DataGrid1.DataSource: = GetOrders (calendar1.selecteddate);
Datagrid1.databind;
End

Procedure Button1Click (Sender:System.Object; E:eventargs);
Begin
Calendar1.visibledate: = System.Convert.ToDateTime (Edit1.text);
Label1.Text: = prodname + ' says you set ' + Calendar1.VisibleDate.ToString (' D ');
End

Procedure Button2click (Sender:System.Object; E:eventargs);
Begin
Displayfields.text: = Dispfields;
End

function GetOrders (date:datetime): DataSet;
Var
Adapter:sqldataadapter;
Begin
Adapter: = Sqldataadapter.create (
' SELECT ' + Displayfields.text + ' from Orders ' +
' Where OrderDate = ' + date. ToString (' d ') + ' ",
' Server= (local);D atabase=northwind; Trusted_connection=yes ');
Result: = Dataset.create;
Adapter.fill (result);
End

</script>

<body style= "font:18pt Verdana" >
<form runat= "Server" >
<table>
&LT;TR valign= "Top" ><td>
<p><b>pick a date</b></p>
<asp:calendar id= "Calendar1" runat= "Server" forecolor= "#0000FF" backcolor= "#FFFFCC"
onselectionchanged= "dateselected" >
<todaydaystyle font-bold= "True"/>
<nextprevstyle forecolor= "#FFFFCC"/>
<dayheaderstyle backcolor= "#FFCC66"/>
<selecteddaystyle forecolor= "Black" backcolor= "#CCCCFF"/>
<titlestyle font-size= "14pt" font-bold= "True" forecolor= "#FFFFCC" backcolor= "#990000"/>
<othermonthdaystyle forecolor= "#CC9966"/>
</asp:Calendar>
<p><asp:textbox id= "Edit1" width=150 runat= "Server"/>
<asp:button text= "Set date" id= "Button1" runat= "Server"/>
</p>
&LT;/TD&GT;&LT;TD valign= "Top" >
<p><b>display fields:</b> <asp:textbox id= "Displayfields"
Text= "OrderID, CustomerID, ShipName, ShipCity, ShipCountry" width=500 runat= "Server"/>
<asp:button text= "Reset fields" id= "Button2" runat= "Server"/></p>
<asp:datagrid id= "DATAGRID1" runat= "Server" bordercolor= "#FFCC66" forecolor= "#0000FF" >
</asp:DataGrid>
</td></tr></table>
<p><asp:label id= "Label1" runat= "Server"/></p>
</form>
</body>

Whenever the user clicks on the calendar to select a date, the OnSelectionChanged event is triggered and the dateselected () function is invoked.
In the GetOrders function, the connection of the database is implemented by the SqlClient of the namespace definition, and the database is the Model library Northwind for Ms SQL 2000. SqlDataAdapter installs the query results into a DataGrid, displaying data tables, such as drawings.

Change the domain name of the field input box, then click on the calendar, get different results.

This is the calendar-driven database query system, by Delphi for. NET and asp.net together to complete.

Conclusion
This paper tries to explain briefly how to use Delphi in ASP.net.
Keep in mind that this example is a preview example of Delphi 7, and the results of the official version may not be exactly the case, of course.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.