回傳資料處理範例
這個範例示範自訂文字方塊,它具有因回傳而變更的文字屬性。控制項載入回傳資料之後,會引發 TextChanged
事件。若要建置範例,請參閱伺服器控制項範例中的說明。
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
namespace CustomControls{
public class MyTextBox: Control, IPostBackDataHandler {
public String Text {
get {
return (String) ViewState["Text"];
}
set {
ViewState["Text"] = value;
}
}
public event EventHandler TextChanged;
public virtual bool LoadPostData(string postDataKey,
NameValueCollection values) {
String presentValue = Text;
String postedValue = values[postDataKey];
if (!presentValue.Equals(postedValue)){
Text = postedValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent() {
OnTextChanged(EventArgs.Empty);
}
protected virtual void OnTextChanged(EventArgs e){
if (TextChanged != null)
TextChanged(this,e);
}
protected override void Render(HtmlTextWriter output) {
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Specialized
Namespace CustomControls
Public Class MyTextBox
Inherits Control
Implements IPostBackDataHandler
Public Property Text() As String
Get
Return CType(ViewState("Text"), String)
End Get
Set
ViewState("Text") = value
End Set
End Property
Public Event TextChanged As EventHandler
Public Overridable Function LoadPostData(postDataKey As String, values As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
Dim presentValue As String = Text
Dim postedValue As String = values(postDataKey)
If Not presentValue.Equals(postedValue) Then
Text = postedValue
Return True
End If
Return False
End Function
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
OnTextChanged(EventArgs.Empty)
End Sub
Protected Overridable Sub OnTextChanged(e As EventArgs)
RaiseEvent TextChanged(Me, e)
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.AddAttribute(HtmlTextWriterAttribute.Type, "text")
output.AddAttribute(HtmlTextWriterAttribute.Value, me.Text)
output.AddAttribute(HtmlTextWriterAttribute.Name, me.UniqueID)
output.RenderBeginTag(HtmlTextWriterTag.Input)
output.RenderEndTag()
End Sub
End Class
End Namespace
在網頁上使用控制項
下列 ASP.NET 網頁使用前面範例定義的自訂控制項 MyTextBox
。
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="C#" runat=server>
private StringBuilder message = new StringBuilder("");
private void Text_Changed(Object sender,EventArgs e){
message.Append("The text in" + sender.ToString()+ " was changed.");
message.Append("<br>You entered " + Server.HtmlEncode(Box.Text) +".");
}
protected override void Render(HtmlTextWriter output) {
base.Render(output);
output.Write(message.ToString());
}
</script>
<body>
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>
Enter your name: <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box" runat=server/>
<br><br>
<asp:Button Text = "Submit" runat = server/>
</form>
</body>
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>
<script language="VB" runat=server>
Private message As New StringBuilder("")
Private Sub Text_Changed(sender As Object, e As EventArgs)
message.Append(("The text in" & sender.ToString() & " was changed."))
message.Append(("<br>You entered " & Server.HtmlEncode(Box.Text) & "."))
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
MyBase.Render(output)
output.Write(message.ToString())
End Sub
</script>
<body>
<form method="POST" action="MyTextBox.aspx" runat=server>
The text box below is an instance of a custom text box.<br>
It raises a text changed event when the text in the text box changes.<br><br>
Enter your name: <Custom:MyTextBox Text=" " OnTextChanged = "Text_Changed" id = "Box" runat=server/>
<br><br>
<asp:Button Text = "Submit" runat = server/>
</form>
</body>
</html>