Thursday, August 12, 2010

How to create a custom TextBox control

There are couple of Classes and interfaces that you can use:(inherit)
  1. WebControl
  2. IPostbackDataHanlder
  3. INamingContainer
Example:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class CustomTextBox : WebControl,
                                IPostBackDataHandler,
                                INamingContainer
{
    public string Text
    {
        get
        {
            return (string)(ViewState["Text"] ?? (ViewState["Text"] = string.Empty));
        }
 
        set
        {
            ViewState["Text"] = value;
        }
    }
    public bool AutoPostBack { getset; }
    public event EventHandler TextChanged;
    public virtual bool LoadPostData(string postDataKey,
                                    NameValueCollection postCollection)
    {
        string postedValue = postCollection[postDataKey];
        this.Text = postedValue;
        return false;
    }
    public virtual void RaisePostDataChangedEvent()
    {
        OnCheckChanged(EventArgs.Empty);
    }
    protected virtual void OnCheckChanged(EventArgs e)
    {
        if (TextChanged != null)
            TextChanged(this, e);
    }
    protected override void Render(HtmlTextWriter output)
    {
        output.Write(string.Format("<input type='text' id='{0}' name='{1}' value='{2}'{3}  />",
            this.ClientID,
            this.UniqueID,
            this.Text,
            this.AutoPostBack ? string.Format(
                " onclick=\"javascript:__doPostBack('{0}','');\"",
                this.UniqueID) :
                string.Empty));
    }
}
 

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...