TextBox.OnTextChanged(EventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 TextChanged 事件。 這樣你就能直接處理事件。
protected:
virtual void OnTextChanged(EventArgs ^ e);
protected virtual void OnTextChanged(EventArgs e);
abstract member OnTextChanged : EventArgs -> unit
override this.OnTextChanged : EventArgs -> unit
Protected Overridable Sub OnTextChanged (e As EventArgs)
參數
範例
以下程式碼範例示範如何覆寫該 OnTextChanged 方法,使其始終標記 TextBox 自訂伺服器控制項已被修改。
Important
此範例中有一個文字框可接受使用者輸入,這可能構成安全威脅。 預設情況下,ASP.NET 網頁會驗證使用者輸入中不包含腳本或 HTML 元素。 欲了解更多資訊,請參閱 腳本漏洞概述。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom TextBox - OnTextChanged - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom TextBox - OnTextChanged - C# Example</h3>
<aspSample:CustomTextBoxOnTextChanged
id="TextBox1"
autopostback=true
runat="server">Hello World!
</aspSample:CustomTextBoxOnTextChanged>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom TextBox - OnTextChanged - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom TextBox - OnTextChanged - VB.NET Example</h3>
<aspSample:CustomTextBoxOnTextChanged id="TextBox1" autopostback=true
runat="server">Hello World!</aspSample:CustomTextBoxOnTextChanged>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomTextBoxOnTextChanged : System.Web.UI.WebControls.TextBox
{
private bool isDirty = false;
protected override void OnTextChanged(System.EventArgs e)
{
// Call the base OnTextChanged method.
base.OnTextChanged(e);
// Change the dirty flag to True.
isDirty = true;
}
}
}
Imports System.Web
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomTextBoxOnTextChanged
Inherits System.Web.UI.WebControls.TextBox
Private isDirty As Boolean = False
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
' Call the base OnTextChanged method.
MyBase.OnTextChanged(e)
' Change the dirty flag to True.
isDirty = True
End Sub
End Class
End Namespace
備註
TextChanged當伺服器發文時,文字框內容會改變。
Note
必須有一個 TextBox 控制項在貼文間持久化某些值到伺服器,才能讓此事件正常運作。 確保此控制的視圖狀態已啟用。
發起事件會透過代理呼叫事件處理者。 欲了解更多資訊,請參閱 處理與提升事件。
此 OnTextChanged 方法也允許衍生類別在不附加代理的情況下處理事件。 這是在衍生類別中處理事件的首選技術。
給繼承者的注意事項
在導出類別中覆寫 OnTextChanged(EventArgs) 時,務必呼叫基底類別的方法 OnTextChanged(EventArgs) ,讓註冊代理接收事件。