主题:  什么是 ASP.NET Web 窗体?(三)使用自定义服务器

VRFan_5d

职务:普通成员
等级:1
金币:0.0
发贴:7
#12003/5/18 0:01:23
ASP.NET 随附了 45 个可以在框外使用的内置服务器控件(有关详细信息,请参阅 Web 窗体控件引用)。除了使用内置 ASP.NET 控件外,开发人员还可使用由第三方供应商开发的控件。

下面的示例显示一个简单的日历控件。Calendar 控件在页内由 标记声明。注意,页顶部的 <% Register %> 指令负责向控件实现的“Acme”代码命名空间注册“Acme”XML 标记前缀。ASP.NET 页分析器然后将在运行时利用该命名空间加载 Calendar 控件类实例。
C#:Intro7.aspx
--------------------------------------------------------------------------------------------------
<%@ Register TagPrefix="Acme" Namespace="Acme" Assembly="Acme" %>

















姓名:

类别:
psychology
business
popular_comp




















--------------------------------------------------------------------------------------------------
Acme.cs
--------------------------------------------------------------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("快速入门教程汇编")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("Microsoft QuickStart Tutorials")]
[assembly: AssemblyCopyright("Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

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 OnInit(EventArgs E)
{
Page.RegisterRequiresPostBack(this);

currentDate = DateTime.Now;

monthNames[0] = "一月";
monthNames[1] = "二月";
monthNames[2] = "三月";
monthNames[3] = "四月";
monthNames[4] = "五月";
monthNames[5] = "六月";
monthNames[6] = "七月";
monthNames[7] = "八月";
monthNames[8] = "九月";
monthNames[9] = "十月";
monthNames[10] = "十一月";
monthNames[11] = "十二月";
}

protected override void LoadViewState(Object viewState)
{
// 如果我们执行了回发,则可以使用以前的日期

if (null != viewState)
{
currentDate = DateTime.Parse((String) viewState);
}
}

public void RaisePostBackEvent(String eventArgument)
{
//Page.Response.Write("调用了 RaisePostBackEvent!!!");

if (eventArgument == null)
{
return;
}

// 跟踪以前的日期(用于引发事件)

DateTime oldDate = currentDate;

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 SaveViewState()
{
// 将 CurrentDate 作为回发方案的视图状态保存出来

return currentDate.ToString();
}

protected override void Render(HtmlTextWriter output)
{
if ((Page.Request.UserAgent != null)&&(Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1))
RenderUpLevel(output);
else
RenderDownLevel(output);
}

protected void RenderUpLevel(HtmlTextWriter output)
{
output.WriteLine("");
output.WriteLine("");
output.WriteLine("");
}

protected override void OnPreRender(EventArgs E)
{
String DHTMLFunction = "";

DHTMLFunction += "\n";

if ((Page.Request.UserAgent != null)&&(Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1))
Page.RegisterClientScriptBlock("ACME_CALENDAR_DHTML", DHTMLFunction);
}

protected void RenderDownLevel(HtmlTextWriter output)
{
// 输出日历标题

output.WriteLine("
");
output.WriteLine("");
output.WriteLine("");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine(" ");
output.WriteLine("
");
output.WriteLine(" " + monthNames[currentDate.Month-1] + " " + currentDate.Year.ToString() + "");
output.WriteLine(" ");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine("");
output.WriteLine("");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine("");
output.WriteLine("");

// 计算该月中有多少天

int numDays = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);

// 计算该月的第一天是周几

int firstDay = Convert.ToInt32(new DateTime(currentDate.Year, currentDate.Month, 1).DayOfWeek);

// 准备填充日期

for (int x=0; x {
output.WriteLine("
");
}

// 输出每一天

for (int x=1; x<=numDays; x++)
{
if (currentDate.Day == x)
{
output.Write("");
}
else
{
output.Write("");
}

// 在每页中进行适当地分行
if (((firstDay+x) % 7) == 0)
{
output.WriteLine("");
}
}

output.WriteLine("");
output.WriteLine("
Sun Mon Tue Wed Thu Fri Sat
");
output.Write("" + x.ToString() + "");
output.WriteLine("
");
output.Write("");
output.Write(x.ToString() + "
");
output.WriteLine("
");
}

public DateTime Date
{
get
{
return currentDate;
}
set
{
currentDate = value;
}
}

public String BackColor
{
get
{
return backColor;
}
set
{
backColor = value;
}
}

public String ForeColor
{
get
{
return foreColor;
}
set
{
foreColor = value;
}
}

public bool LoadPostData(String postDataKey, NamevalueCollection values)
{
String clientDate = values[UniqueID + "_CurrentDate"];

if (clientDate != null) {
try {
currentDate = DateTime.Parse(clientDate);
} catch(Exception) {
currentDate = DateTime.Now;
}
}

return false;
}

public void RaisePostDataChangedEvent()
{

}
}
}

--------------------------------------------------------------------------------------------------
ads.xml
--------------------------------------------------------------------------------------------------



/quickstart/aspplus/images/banner1.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner2.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner3.gif
http://www.microsoft.com
Alt Text
Computers
80





VB:Intro7.aspx
--------------------------------------------------------------------------------------------------
<%@ Register TagPrefix="Acme" Namespace="AcmeVB" Assembly="AcmeVB" %>

















姓名:

类别:
psychology
business
popular_comp



















--------------------------------------------------------------------------------------------------
Acme.vb
--------------------------------------------------------------------------------------------------
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections
Imports System.Collections.Specialized
Imports Microsoft.VisualBasic
Imports System.Reflection

< _
Assembly : AssemblyTitle(""), _
Assembly : AssemblyDescription("快速入门教程汇编"), _
Assembly : AssemblyConfiguration(""), _
Assembly : AssemblyCompany("Microsoft Corporation"), _
Assembly : AssemblyProduct("Microsoft QuickStart Tutorials"), _
Assembly : AssemblyCopyright("Microsoft Corporation. All rights reserved."), _
Assembly : AssemblyTrademark(""), _
Assembly : AssemblyCulture(""), _
Assembly : AssemblyVersion("1.0.*"), _
Assembly : AssemblyDelaySign(false), _
Assembly : AssemblyKeyFile(""), _
Assembly : AssemblyKeyName("") _
>
Namespace AcmeVB
Public Class Calendar :
Inherits Control :
Implements IPostBackEventHandler, IPostBackDataHandler
Dim Private monthNames(11) As String
Dim Private currentDate As DateTime = DateTime.Now
Dim private pBackColor As String = "#dcdcdc"
Dim private pForeColor As String = "#eeeeee"

Protected Overrides Sub OnInit(E As EventArgs)
Page.RegisterRequiresPostBack(Me)

currentDate = DateTime.Now

monthNames(0) = "一月"
monthNames(1) = "二月"
monthNames(2) = "三月"
monthNames(3) = "四月"
monthNames(4) = "五月"
monthNames(5) = "六月"
monthNames(6) = "七月"
monthNames(7) = "八月"
monthNames(8) = "九月"
monthNames(9) = "十月"
monthNames(10) = "十一月"
monthNames(11) = "十二月"
End Sub

Protected Overrides Sub LoadViewState(viewState As Object)
'如果我们执行了回发,则可以使用以前的日期

If Not viewState Is Nothing
currentDate = DateTime.Parse(CStr(viewState))
End If
End Sub

Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
If eventArgument = Nothing Then Exit Sub

'跟踪以前的日期(用于引发事件)

Dim oldDate As DateTime = currentDate

If String.Compare("NavNextMonth", eventArgument, true) = 0
currentDate = currentDate.AddMonths(1)
ElseIf String.Compare("NavPrevMonth", eventArgument, true) = 0
currentDate = currentDate.AddMonths(-1)
Else
Dim daySelected As Integer = Int32.Parse(eventArgument)
currentDate = new DateTime(currentDate.Year, currentDate.Month, daySelected)
End If
End Sub

Protected Overrides Function SaveViewState() As Object
'将 CurrentDate 作为回发方案的视图状态保存出来
SaveViewState = currentDate.ToString()
End Function

Protected Overrides Sub Render(output As HtmlTextWriter)
If ((Not Page.Request.UserAgent = Nothing) AndAlso (Page.Request.UserAgent.IndexOf("MSIE 5.5") <> -1))
RenderUpLevel(output)
Else
RenderDownLevel(output)
End If
End Sub

Protected Sub RenderUpLevel(output As HtmlTextWriter)
output.WriteLine("")
output.WriteLine("")
output.WriteLine("")
End Sub

Protected Overrides Sub OnPreRender(E As EventArgs)
Dim DHTMLFunction As String = ""

DHTMLFunction = DHTMLFunction & "\n"
DHTMLFunction = DHTMLFunction.Replace("\n", Chr(10))

If ((Not Page.Request.UserAgent = Nothing) AndAlso (Page.Request.UserAgent.IndexOf("MSIE 5.5") <> -1))
Page.RegisterClientScriptBlock("ACME_CALENDAR_DHTML", DHTMLFunction)
End If
End Sub

Protected Sub RenderDownLevel(output As HtmlTextWriter)
'输出日历标题

output.WriteLine("
")
output.WriteLine("")
output.WriteLine("")
output.WriteLine("
")
output.WriteLine("
")
output.WriteLine(" ")
output.WriteLine("
")
output.WriteLine(" " & monthNames(currentDate.Month-1) & " " & currentDate.Year.ToString() & "")
output.WriteLine(" ")
output.WriteLine("
")
output.WriteLine("
")
output.WriteLine("
")
output.WriteLine("")
output.WriteLine("")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine(" ")
output.WriteLine("")
output.WriteLine("")

'计算该月中有多少天

Dim numDays As Integer = DateTime.DaysInMonth(currentDate.Year, currentDate.Month)

'计算该月的第一天是周几

Dim firstDay As Integer = New DateTime(currentDate.Year, currentDate.Month, 1).DayOfWeek

'准备填充日期
Dim x As Integer

For x = 0 To firstDay - 1
output.WriteLine("")
Next

'输出每一天

For x = 1 To numDays
If currentDate.Day = x
output.Write("")
Else
output.Write("")
End If

'在每页中进行适当地分行
If ((firstDay+x) Mod 7) = 0
output.WriteLine("")
End If
Next

output.WriteLine("")
output.WriteLine("
Sun Mon Tue Wed Thu Fri Sat
")
output.Write("" & x.ToString() & "")
output.WriteLine("
")
output.Write("")
output.Write(x.ToString() & "
")
output.WriteLine("
")
End Sub

Public Property [Date] As DateTime
Get
Return currentDate
End Get
Set
currentDate = value
End Set
End Property

Public Property BackColor As String
Get
Return pBackColor
End Get
Set
pBackColor = value
End Set
End Property

Public Property ForeColor As String
Get
Return pForeColor
End Get
Set
pForeColor = value
End Set
End Property

Public Function LoadPostData(postDataKey As String, values As NamevalueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
Dim clientDate As String = values(UniqueID & "_CurrentDate")

If Not clientDate = Nothing
Try
currentDate = DateTime.Parse(clientDate)
Catch
currentDate = DateTime.Now
End Try
End If

Return False
End Function

Public Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
End Sub
End Class
End Namespace

--------------------------------------------------------------------------------------------------
ads.xml
--------------------------------------------------------------------------------------------------



/quickstart/aspplus/images/banner1.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner2.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner3.gif
http://www.microsoft.com
Alt Text
Computers
80





--------------------------------------------------------------------------------------------------
js:Intro7.aspx
--------------------------------------------------------------------------------------------------
<%@ Register TagPrefix="Acme" Namespace="AcmeJS" Assembly="AcmeJS" %>
















姓名:

类别:
psychology
business
popular_comp




















--------------------------------------------------------------------------------------------------
Acme.js
--------------------------------------------------------------------------------------------------
import System;
import System.Collections;
import System.Collections.Specialized;
import System.Text;
import System.Web;
import System.Web.UI;
import System.Reflection;

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("A QuickStart Tutorial Assembly")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("Microsoft QuickStart Tutorials")]
[assembly: AssemblyCopyright("Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

package AcmeJS
{
public class Calendar extends Control implements IPostBackEventHandler, IPostBackDataHandler
{
private var monthNames:String[] = new String[12];
private var currentDate:DateTime = DateTime.Now;
private var backColor:String = "#dcdcdc";
private var foreColor:String = "#eeeeee";

protected function OnInit(E:EventArgs) : void
{
Page.RegisterRequiresPostBack(this);

currentDate = DateTime.Now;

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 function LoadViewState(viewState:Object) : void
{
// 如果我们执行了回发,则可以使用以前日期

if (null != viewState)
{
currentDate = DateTime.Parse(String(viewState));
}
}

public function RaisePostBackEvent(eventArgument:String) : void
{
if (eventArgument == null)
{
return;
}

// 跟踪以前的日期(用于引发事件)

var oldDate:DateTime = currentDate;

if (String.Compare("NavNextMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(1);
}
else if (String.Compare("NavPrevMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(-1);
}
else
{
var daySelected:int = Int32.Parse(eventArgument);
currentDate = new DateTime(currentDate.Year, currentDate.Month, daySelected);
}
}

protected function SaveViewState() : Object
{
// 将 CurrentDate 作为回发方案的视图状态保存出来

return currentDate.ToString();
}

protected function Render(output:HtmlTextWriter) : void
{
if ((Page.Request.UserAgent != null)&&(Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1))
RenderUpLevel(output);
else
RenderDownLevel(output);
}

protected function RenderUpLevel(output:HtmlTextWriter) : void
{
output.WriteLine("");
output.WriteLine("");
output.WriteLine("");
}

protected function OnPreRender(E:EventArgs) : void
{
var DHTMLFunction:StringBuilder = new StringBuilder();

DHTMLFunction.Append("\n");

if ((Page.Request.UserAgent != null)&&(Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1))
Page.RegisterClientScriptBlock("ACME_CALENDAR_DHTML", DHTMLFunction.ToString());
}

protected function RenderDownLevel(output:HtmlTextWriter) : void
{
// 输出日历标题

output.WriteLine("
");
output.WriteLine("");
output.WriteLine("");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine(" ");
output.WriteLine("
");
output.WriteLine(" " + monthNames[currentDate.Month-1] + " " + currentDate.Year.ToString() + "");
output.WriteLine(" ");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine("
");
output.WriteLine("");
output.WriteLine("");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine(" ");
output.WriteLine("");
output.WriteLine("");

// 计算该月中有多少天

var numDays:int = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);

// 计算该月的第一天是周几

var firstDay:int = new DateTime(currentDate.Year, currentDate.Month, 1).DayOfWeek;

// 准备填充日期
var x:int;
for ( x=0; x {
output.WriteLine("
");
}

// 输出每一天

for ( x=1; x<=numDays; x++)
{
if (currentDate.Day == x)
{
output.Write("");
}
else
{
output.Write("");
}

// 在每页中进行适当地分行
if (((firstDay+x) % 7) == 0)
{
output.WriteLine("");
}
}

output.WriteLine("");
output.WriteLine("
Sun Mon Tue Wed Thu Fri Sat
");
output.Write("" + x.ToString() + "");
output.WriteLine("
");
output.Write("");
output.Write(x.ToString() + "
");
output.WriteLine("
");
}

public function get Date() : DateTime
{
return currentDate;
}
public function set Date(value:DateTime)
{
currentDate = value;
}

public function get BackColor() : String
{
return backColor;
}
public function set BackColor(value:String)
{
backColor = value;
}

public function get ForeColor() : String
{
return foreColor;
}
public function set ForeColor(value:String)
{
foreColor = value;
}

public function LoadPostData(postDataKey:String, values:NamevalueCollection) : Boolean
{
var clientDate:String = values[UniqueID + "_CurrentDate"];

if (clientDate != null) {
try {
currentDate = DateTime.Parse(clientDate);
} catch(e:Exception) {
currentDate = DateTime.Now;
}
}

return false;
}

public function RaisePostDataChangedEvent() : void
{

}
}
}


--------------------------------------------------------------------------------------------------
ads.xml
--------------------------------------------------------------------------------------------------



/quickstart/aspplus/images/banner1.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner2.gif
http://www.microsoft.com
Alt Text
Computers
80



/quickstart/aspplus/images/banner3.gif
http://www.microsoft.com
Alt Text
Computers
80