Saturday, June 05, 2010

How does .Net Framework track Postback values : Asp.Net

When page life cycle executes, for all controls in a page, if a control implements IPostBackDataHandler interface .net framwork invokes, IPostBackDataHandler's LoadPostData(string postKey, NameValueCollection postCollection)

public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
    ///
    /// Process your data here
    ///
    return false;
}

postCollection:
Is a NameValueCollection which consist of all input filelds present in the page along with their values agaisnt corrosponding names.
  • ctrl00$txtFirstName - FirstName
  • ctrl00$txtSurname - Surname
  • ctrl00$txtAddress - Address
Remember .net framwork use a format of ctl00_txtName as the id of the dom element. (.net control.ClientID) and exactly the same but separater is $ for name, ctrl00$txtName. (.net control UniqueID)
postKey
Is the name of the corresponding control. So easyly we can retrive the value of the input field like String postedValue = postCollection[postDataKey];
In terms of writting HTML, when you write a html, you cant just use Response.Write("<input..... ) but you can write a simple web-control (in your case checkbox) and add instance of your custom check box to the page. Unless the control get added to the page, page life cycle wont be able to do anyting with your control. For build a simple web-control you can just extend WebControl class in System.Web.UI and to handle postbacks you may implement IPostBackDataHandler interface as well.
Example:

CustomCheckBox cchkBox = new CustomCheckBox();
this.Page.Controls.Add(cchkBox);

This is a complete example of Custom Checkbox which has a hidden field to hold it's value.

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class CustomCheckBox : WebControl,
                                IPostBackDataHandler,
                                INamingContainer
{
    public bool Checked
    {
        get
        {
            return (bool)(ViewState["Checked"] ?? (ViewState["Checked"] = false));
        }
 
        set
        {
            ViewState["Checked"] = value;
        }
    }
    public bool AutoPostBack { getset; }
    public event EventHandler CheckChanged;
    public string Label { getset; }
    public virtual bool LoadPostData(string postDataKey,
                                    NameValueCollection postCollection)
    {
        bool presentValue = this.Checked;
        String postedValue = postCollection[postDataKey];
        if (!presentValue.Equals(postedValue))
        {
            this.Checked = postedValue.Equals("on") ? true : false;
            return true;
        }
        return false;
    }
    public virtual void RaisePostDataChangedEvent()
    {
        OnCheckChanged(EventArgs.Empty);
    }
    protected virtual void OnCheckChanged(EventArgs e)
    {
        if (CheckChanged != null)
            CheckChanged(this, e);
    }
    protected override void Render(HtmlTextWriter output)
    {
        output.Write(string.Format("<input type='checkbox' {0}{1} />",
            this.Checked ? " checked='checked'" : string.Empty,
            this.AutoPostBack ? string.Format(
                " onclick=\"javascript:__doPostBack('{0}','');\"",
                this.UniqueID) :
            string.Empty));
        output.Write(string.Format("<span>{0}</span>"this.Label));
        output.Write(string.Format("<input type='hidden' value='{0}' id='{1}' name='{2}' />",
            this.Checked ? "off" : "on"this.ClientID, this.UniqueID));
    }
}

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...