TextBox TextChanged事件触发时? [英] When TextBox TextChanged event is fired?

查看:136
本文介绍了TextBox TextChanged事件触发时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:

据我们所知,ViewState不负责存储和还原TextBox,CheckBox和这样的控件的值。这是通过LoadPostData()方法完成的,用于控制实现IPostBackDataHandler接口。

As we know ViewState is not responsible for storing and restoring TextBox,CheckBox and such controls Values. This is done by LoadPostData() method to controls that implement IPostBackDataHandler interface.

而且我们也知道在Load stage之后,会发生RaisePostBackEvent阶段并引发相应的事件,如Button Click或If TextBox中的文本已更改,其TextChanged事件将被触发。

And we also know after Load stage,RaisePostBackEvent stage occurs and raise corresponding events such Button Click or if Text changed in a TextBox, its TextChanged event will be fired.

那么如果ViewState不负责,那么系统跟踪文本会如何更改,哪个机制实际上会触发TextBox TextChanged事件?

So how does system track the text changed if ViewState is not responsible for that and which mechanism actually fires TextBox TextChanged event ?

我实际上在这一点上感到困惑。

I am actually confused at this point.

提前感谢。

推荐答案

我认为它是这样工作的:

I think it is working in this way :

控制实现IPostBackDataHandler而不是IPostBackEventHandler,因为它被文本状态触发。因此,如果在已确定的任何更改中发生更改

TextBox control implements IPostBackDataHandler instead of IPostBackEventHandler because it's fired by its text state. So if any changes happened in postedValue which is determined

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

部分然后它返回true并继续执行,所以TextChanged被触发。 Pff令人困惑,但看起来很容易。

portion then it returns true and keep executing so finally TextChanged fired. Pff confusing but looks easy tho.

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 + " >");
  }
   }   
}

这篇关于TextBox TextChanged事件触发时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆