撰寫和呈現的比較
雖然撰寫複合控制項相當簡單,但撰寫會耗用效能,因為它必須建立子控制項。如果您想要控制項有最佳效能,您可以自己覆寫 Render 方法來實作呈現邏輯。此外,您必須實作控制項所需要的任何回傳資料處理和回傳事件處理。
下列範例示範控制項 Rendered
,它會呈現相等於複合伺服器控制項範例所提供使用者介面的使用者介面。Rendered
不使用撰寫,因而撰寫起來更為複雜。
下列 Rendered
的成員等同於複合伺服器控制項範例中的成員。
- 公用
Text
屬性。(然而,這個案例中,屬性不委派給子控制項)。 - 公用
Number
屬性。 - 私用
Sum
屬性。 - 自訂
Check
事件。
因為 Rendered
沒有子控制項,它必須進行下列額外工作。
- 為文字方塊提供它自己的實作 (Implementation)。
- 為送出鈕提供它自己的實作。
- 實作 IPostBackEventHandler。
- 實作 IPostBackDataHandler。
- 覆寫 Render 方法。
請注意,Rendered
不使用 ViewState 跨數趟往返來保存屬性,因為狀態還原在這個案例中並不需要。也請注意,在這個範例中,Render 方法將未經處理的 HTML 寫入 HtmlTextWriter 的 Write 方法。如需使用 HtmlTextWriter 的公用程式方法來呈現的範例,請參閱呈現伺服器控制項範例。
若要建置範例,請參閱伺服器控制項範例中的說明。
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
namespace CustomControls
{
public class Rendered : Control, IPostBackDataHandler, IPostBackEventHandler
{
private String text1;
private String text2;
private String text = "Press the button to see if you won.";
private int number = 100;
private int Sum
{
get
{
return Int32.Parse(text1) +
Int32.Parse(text2);
}
}
public int Number
{
get
{
return number;
}
set
{
number = value;
}
}
public String Text {
get {
return text;
}
set {
text = value;
}
}
public event CheckEventHandler Check;
protected virtual void OnCheck(CheckEventArgs ce)
{
if (Check != null)
{
Check(this,ce);
}
}
public virtual bool LoadPostData(string postDataKey,
NameValueCollection values) {
text1 = values[UniqueID + "t1"];
text2 = values[UniqueID+ "t2"];
Page.RegisterRequiresRaiseEvent(this);
return false;
}
public virtual void RaisePostDataChangedEvent() {
}
public void RaisePostBackEvent(string eventArgument){
OnCheck(new CheckEventArgs(Sum - Number));
}
protected override void Render(HtmlTextWriter output) {
output.Write ("<h3>Enter a number : ");
output.Write("<INPUT type= text name = " + this.UniqueID + "t1" +
" value = '0' >");
output.Write("</h3><br>");
output.Write ("<br><h3>Enter another number : ");
output.Write("<INPUT type= text name = " + this.UniqueID + "t2" +
" value = '0' >");
output.Write("</h3><br>");
output.Write("<br><br><input type= submit name = " +
this.UniqueID + " value= 'Submit'>");
output.Write("<br><br><span style='height:50px;width:500px;'>"
+ Text + "</span>");
}
}
}
// CheckEvent.cs.
// Contains the code for the custom event data class CheckEventArgs.
// Also defines the event handler for the Check event.
using System;
namespace CustomControls
{
public class CheckEventArgs : EventArgs
{
private bool match = false;
public CheckEventArgs (int difference)
{
if (difference == 0)
{
match = true;
}
}
public bool Match
{
get
{
return match;
}
}
}
public delegate void CheckEventHandler(object sender, CheckEventArgs ce);
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Specialized
Namespace CustomControls
Public Class Rendered
Inherits Control
Implements IPostBackDataHandler, IPostBackEventHandler
Private text1 As String
Private text2 As String
Private _text As String = "Press enter to see if you won."
Private _number As Integer = 100
Private ReadOnly Property Sum() As Integer
Get
Return Int32.Parse(text1) + Int32.Parse(text2)
End Get
End Property
Public Property Number() As Integer
Get
Return _number
End Get
Set
_number = value
End Set
End Property
Public Property Text() As String
Get
Return _text
End Get
Set
_text = value
End Set
End Property
Public Event Check As CheckEventHandler
Protected Overridable Sub OnCheck(ce As CheckEventArgs)
RaiseEvent Check(Me, ce)
End Sub
Public Overridable Function LoadPostData(postDataKey As String, values As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
text1 = values(UniqueID & "t1")
text2 = values(UniqueID & "t2")
Page.RegisterRequiresRaiseEvent(Me)
Return False
End Function
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
End Sub
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
OnCheck(New CheckEventArgs(Sum - Number))
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("<h3>Enter a number : ")
output.Write("<INPUT type= text name = " & _
Me.UniqueID & "t1" & _
" value = '0' >")
output.Write("</h3><br>")
output.Write("<br><h3>Enter another number : ")
output.Write("<INPUT type= text name = " & _
Me.UniqueID & "t2" & _
" value = '0' >")
output.Write("</h3><br>")
output.Write("<br><br><input type= submit name = " & _
Me.UniqueID & _
" value= 'Submit'>")
output.Write("<br><br><span style='height:50px;width:500px;'>" & _
Text & "</span>")
End Sub
End Class
End Namespace
' CheckEvent.vb
' Contains the code for the custom event data class CheckEventArgs.
' Also defines the event handler for the Check event.
Imports System
Namespace CustomControls
Public Class CheckEventArgs
Inherits EventArgs
Private _match As Boolean = False
Public Sub New(difference As Integer)
If difference = 0 Then
_match = True
End If
End Sub
Public ReadOnly Property Match() As Boolean
Get
Return _match
End Get
End Property
End Class
Public Delegate Sub CheckEventHandler(sender As Object, ce As CheckEventArgs)
End Namespace
在網頁上使用呈現的控制項
下列網頁會使用前面範例中定義的自訂控制項 Rendered
。請注意,這個網頁等同於複合伺服器控制項範例 (使用複合控制項而非呈現的控制項) 中的網頁。
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="VB" runat=server>
Private Sub Sum_Checked(sender As Object, e As CheckEventArgs)
If e.Match = True Then
Rendered.Text = "<h2> You won a million dollars!!!! </h2>"
Else
Rendered.Text = "Sorry, try again. The numbers you entered don't add up to" & _
" the hidden number."
End If
End Sub
</script>
<html>
<body>
<h1> The Mystery Sum Game </h1><br>
<form runat=server>
<Custom:Rendered id = "Rendered" OnCheck = "Sum_Checked" Number= "10" runat = server/>
</form>
</body>
</html>