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:

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...