使用定制的服务器控件
在ASP+中提供了45个已经做好了的服务器控件,我们可以将他们象黑盒子一样的使用。除此以外,开发者还可以使用任何第三方开发的服务器控件
在下面的例子中,我们要用到一个通过<acme:calendar runat=server>标签声明的组件,请注意,在文件的第一行必须使用<% Register %> 来声明 "Acme XML "标签的的前缀"Acme".在ASP+ 文件中,将会使用这个命名的服务器控件的类的实例。
<%@ Register TagPrefix="Acme" Namespace="Acme" %>
<HTML>
<head>
<link rel="stylesheet"href="intro.CSS">
</head>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hi " & Name.Text & ", you selected: " & Category.SelectedItem.Text & " on: " & MyCal.Date
End Sub
</script>
<body>
<center>
<form action="intro7.ASPx" method="post" runat="server">
<ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <ASP:textbox id="Name" runat="server"/>
Category: <ASP:dropdownlist id="Category" runat=server>
<ASP:listitem >psychology</ASP:listitem>
<ASP:listitem >business</ASP:listitem>
<ASP:listitem >popular_comp</ASP:listitem>
</ASP:dropdownlist>
<ASP:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<Acme:Calendar id="MyCal" runat=server/>
<p>
<ASP:label id="Message" runat="server"/>
</form>
</center>
</body>
</HTML>
声明Acme 的 文件
using System;
using System.Web;
using System.Web.UI;
using System.Collections;
namespace Acme
{
public class Calendar : Control, IPostBackEventHandler, IPostBackDataHandler
{
private String[] monthNames = new String[12];
private DateTime currentDate = DateTime.Now;
private String backColor = "#dcdcdc";
private String foreColor = "#eeeeee";
protected override void Init()
{
// Todo: We should remove the need to call this
Page.RegisterRequiresPostBack(this);
Page.RegisterPostBackScript();
currentDate = DateTime.Now;
// Todo: Hack needed because COOL doesn't support array initializers yet
monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";
}
protected override void LoadState(Object viewState)
{
// If we've done a post-back, the old date will be available to us
if (null != viewState)
{
currentDate = DateTime.Parse((String) viewState);
}
}
public void RaisePostBackEvent(String eventArgument)
{
//Page.Response.Write("RaisePostBackEvent Called!!!");
if (eventArgument == null)
{
return;
}
// Keep track of old date (for event firing purposes)
DateTime oldDate = currentDate;
// Todo: We should have post-back take two arguments: eventname and eventarguments
if (String.Compare("NavNextMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(1);
}
else if (String.Compare("NavPrevMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(-1);
}
else
{
int daySelected = Int32.Parse(eventArgument);
currentDate = new DateTime(currentDate.Year, currentDate.Month, daySelected);
}
}
protected override Object SaveState()
{
// Save CurrentDate out as view state for postback scenarios
return currentDate.ToString();
}
protected override void Render(HT