C# 何时触发TextBox TextChanged事件?
我的问题是: 正如我们所知,ViewState不负责存储和恢复TextBox、CheckBox等控件的值。这是通过LoadPostData方法对实现IPostBackDataHandler接口的控件执行的 我们还知道在加载阶段之后,RaisePostBackEvent阶段发生,并引发相应的事件,如按钮单击,或者如果文本框中的文本发生了更改,则会触发其TextChanged事件 那么,如果ViewState不负责更改文本,系统如何跟踪更改的文本,以及哪种机制实际触发TextBox TextChanged事件 实际上,我在这一点上感到困惑C# 何时触发TextBox TextChanged事件?,c#,asp.net,events,C#,Asp.net,Events,我的问题是: 正如我们所知,ViewState不负责存储和恢复TextBox、CheckBox等控件的值。这是通过LoadPostData方法对实现IPostBackDataHandler接口的控件执行的 我们还知道在加载阶段之后,RaisePostBackEvent阶段发生,并引发相应的事件,如按钮单击,或者如果文本框中的文本发生了更改,则会触发其TextChanged事件 那么,如果ViewState不负责更改文本,系统如何跟踪更改的文本,以及哪种机制实际触发TextBox TextChan
提前谢谢 我认为它是这样工作的: TextBox控件实现IPostBackDataHandler而不是IPostBackEventHandler,因为它是由其文本状态激发的。所以如果postedValue中发生了任何变化
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 + " >");
}
}
}
跟踪和保存控件基本信息的可能是ControlState,但如果它是控件状态,那么为什么IPostBackDataHandler由TextBox实现?因此,每当回发数据更改时,它也会更改ControlState。