tags:

views:

628

answers:

1

My question is :

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.

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.

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.

Thanks in advance.

A: 

I think it is working in this way :

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;
         }

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 + " >");
  }
   }   
}
Braveyard
So whenever a postback data changes, it will change the controlstate also.
Braveyard