簡體   English   中英

當觸發TextBox TextChanged事件時?

[英]When TextBox TextChanged event is fired?

我的問題是:

眾所周知,ViewState不負責存儲和還原TextBox,CheckBox以及此類控件的Values。 這是通過LoadPostData()方法完成的,以實現IPostBackDataHandler接口的控件。

而且我們還知道,在Load階段之后,會發生RaisePostBackEvent階段,並引發相應的事件,例如Button Click或如果TextBox中的Text更改,則會觸發其TextChanged事件。

那么,如果ViewState不負責文本更改,那么系統如何跟蹤更改的文本?哪種機制實際上會觸發TextBox TextChanged事件?

在這一點上,我實際上很困惑。

提前致謝。

我認為它是以這種方式工作的:

TextBox控件實現了IPostBackDataHandler而不是IPostBackEventHandler,因為它是由其文本狀態觸發的。 因此,如果確定了postValue發生了任何變化,

if (presentValue == null || !presentValue.Equals(postedValue)) {
            Text = postedValue;
            return true;
         } 

然后返回true並繼續執行,因此最終將TextChanged觸發。 PFF令人困惑,但看起來很容易。

using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;


namespace CustomWebFormsControls {

   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
   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 postCollection) {

     String presentValue = Text;
     String postedValue = postCollection[postDataKey];

     if (presentValue == null || !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.Write("<INPUT type= text name = "+this.UniqueID
        + " value = " + this.Text + " >");
  }
   }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM