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 伺服器控制項標示為已修改。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ 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當文字方塊的內容在貼文到伺服器之間變更時,就會引發 事件。
注意
控制項 TextBox 必須在貼文到伺服器之間保存一些值,此事件才能正常運作。 請確定此控制項已啟用檢視狀態。
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件。
OnTextChanged 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnTextChanged(EventArgs) 時,請確定呼叫基底類別的 OnTextChanged(EventArgs) 方法,使已註冊的委派能接收到事件。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應