Delphi 7前瞻:将Delphi作为ASP.NET的脚本语言(下)

王朝asp·作者佚名  2006-01-08
宽屏版  字体: |||超大  

更高技术的网页

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

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

浏览日历然后选择日期;

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

<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>

<h1>Delphi for .NET running in ASP.NET</h1>

<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" OnClick="Button1Click" runat="server" />

</p>

<p><asp:Label id="Label1" runat="server"/></p>

</center>

</form>

</body>

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

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

数据处理

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

<%@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);Database=Northwind;Trusted_Connection=yes');

Result := DataSet.Create;

Adapter.Fill(Result);

end;

</script>

<body style="font:18pt Verdana">

<form runat="server">

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

<table>

<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" OnClick="Button1Click" runat="server" />

</p>

</td><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" OnClick="Button2Click" runat="server" /></p>

<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#FFCC66" ForeColor="#0000FF">

<HeaderStyle ForeColor="#FFFFCC" BackColor="#990000"/>

</asp:DataGrid>

</td></tr></table>

<p><asp:Label id="Label1" runat="server"/></p>

</form>

</body>

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

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

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

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

结语

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有